Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Ember.js{{render}}辅助模型未正确设置_Javascript_Ember.js_Ember Data - Fatal编程技术网

Javascript Ember.js{{render}}辅助模型未正确设置

Javascript Ember.js{{render}}辅助模型未正确设置,javascript,ember.js,ember-data,Javascript,Ember.js,Ember Data,我遇到了一个奇怪的问题。我有一个文章模板。在文章模板中,我调用{{render“category/new”category} 但是,当通过操作保存新类别时,使用了错误的模型。当我将其更改为{{render“category/new”this}}时,它使用文章模型。当我将模型零件留空时,它也不起作用 模板: <script type="text/x-handlebars" data-template-name="article"> ((...)) {{render "c

我遇到了一个奇怪的问题。我有一个文章模板。在文章模板中,我调用
{{render“category/new”category}

但是,当通过操作保存新类别时,使用了错误的模型。当我将其更改为
{{render“category/new”this}}
时,它使用文章模型。当我将模型零件留空时,它也不起作用

模板

<script type="text/x-handlebars" data-template-name="article">
    ((...))
    {{render "category/new" category}} // calls the popup for adding a new category
    ((...))
</script>

<!-- popups -->
<script type="text/x-handlebars" data-template-name="category/new">
    <div class="popup">
        <h2>Add new category</h2>

        Name: {{input type="text" value=name}}<br />
        Image: {{view App.UploadFile name="image" file=image }}<img {{bind-attr src=image}}><br />
        Category-parent: {{input value=categoryRelation}}<br />

        <button {{action 'saveCategory'}}>Save</button>
        </div>
</script>
// both routes have the render called, it uses the same template
this.resource('article', {path: '/article/:id'}); 
this.resource('article.new', {path: "/article/new"});
App.Category = DS.Model.extend({
    name: DS.attr('string'),
    image: DS.attr('string'),
    categoryRelation: DS.belongsTo('category')
});

App.Article = DS.Model.extend({
    name: DS.attr('string'),
    category: DS.hasMany('category')
)};
App.CategoryNewController = Ember.ObjectController.extend({
    actions: {
        saveCategory: function () {
            console.log('CategoryNewController saveCategory action'); // gets called
            console.log(this.get('model')); // the wrong one
            this.get('model').save(); // saves all categories when using {{render "category/new" category}} 
        }
    }
});
模型

<script type="text/x-handlebars" data-template-name="article">
    ((...))
    {{render "category/new" category}} // calls the popup for adding a new category
    ((...))
</script>

<!-- popups -->
<script type="text/x-handlebars" data-template-name="category/new">
    <div class="popup">
        <h2>Add new category</h2>

        Name: {{input type="text" value=name}}<br />
        Image: {{view App.UploadFile name="image" file=image }}<img {{bind-attr src=image}}><br />
        Category-parent: {{input value=categoryRelation}}<br />

        <button {{action 'saveCategory'}}>Save</button>
        </div>
</script>
// both routes have the render called, it uses the same template
this.resource('article', {path: '/article/:id'}); 
this.resource('article.new', {path: "/article/new"});
App.Category = DS.Model.extend({
    name: DS.attr('string'),
    image: DS.attr('string'),
    categoryRelation: DS.belongsTo('category')
});

App.Article = DS.Model.extend({
    name: DS.attr('string'),
    category: DS.hasMany('category')
)};
App.CategoryNewController = Ember.ObjectController.extend({
    actions: {
        saveCategory: function () {
            console.log('CategoryNewController saveCategory action'); // gets called
            console.log(this.get('model')); // the wrong one
            this.get('model').save(); // saves all categories when using {{render "category/new" category}} 
        }
    }
});
控制器

<script type="text/x-handlebars" data-template-name="article">
    ((...))
    {{render "category/new" category}} // calls the popup for adding a new category
    ((...))
</script>

<!-- popups -->
<script type="text/x-handlebars" data-template-name="category/new">
    <div class="popup">
        <h2>Add new category</h2>

        Name: {{input type="text" value=name}}<br />
        Image: {{view App.UploadFile name="image" file=image }}<img {{bind-attr src=image}}><br />
        Category-parent: {{input value=categoryRelation}}<br />

        <button {{action 'saveCategory'}}>Save</button>
        </div>
</script>
// both routes have the render called, it uses the same template
this.resource('article', {path: '/article/:id'}); 
this.resource('article.new', {path: "/article/new"});
App.Category = DS.Model.extend({
    name: DS.attr('string'),
    image: DS.attr('string'),
    categoryRelation: DS.belongsTo('category')
});

App.Article = DS.Model.extend({
    name: DS.attr('string'),
    category: DS.hasMany('category')
)};
App.CategoryNewController = Ember.ObjectController.extend({
    actions: {
        saveCategory: function () {
            console.log('CategoryNewController saveCategory action'); // gets called
            console.log(this.get('model')); // the wrong one
            this.get('model').save(); // saves all categories when using {{render "category/new" category}} 
        }
    }
});

请注意,Category/new没有路由,因为{{render}}助手不需要它。请参阅:(参见页面底部的表格)

关于文章,
类别是一个
有很多
,因此您将
类别NewController的模型设置为一个
类别
数组(除非您遗漏了一个部分,当用户单击“新建”或其他内容时,它会创建一个
类别
对象。)

看起来它应该会起作用。你能设置一个JSFIDLE吗?我开始在这里乱说…:)这是否意味着我必须在
saveCategory
操作中“手动”检索数据?我认为有可能为
{{render}}
助手提供一个单独的模型,如指南中所述。请参阅:您应该在
{render}}
标记:
{{render“category/new”App.category}}
上隐式地将模型作为目标,但我不确定它是否按您希望的方式工作。