Ember.js{{render}}助手模型设置不正确
Ember.js {{render}} helper model not correctly set
我遇到了一个奇怪的问题。我有一个文章模板。在文章模板中,我称之为{{render "category/new" category}}
。
但是,当通过操作保存新类别时,会使用错误的模型。当我将其更改为{{render "category/new" this}}
时,它使用Article模型。当我将模型零件留空时,它也不起作用。
模板:
<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}}帮助程序不需要它。请参阅:http://emberjs.com/guides/templates/rendering-with-helpers/#toc_specific(参见页面底部的表格)
在文章中,category
是has-many
,因此您将CategoryNewController
的模型设置为Category
的数组(除非您在用户单击新的或其他东西时遗漏了创建Category
对象的部分)
相关文章:
- 如何在ExtJs4应用程序上为视图、存储和模型设置自定义文件夹名称
- 是否可以使用flexbox显示模型设置元素的滚动位置
- 从骨干集合筛选模型,然后为这些模型设置属性
- 无法将对象列表从视图模型设置为 javascript 变量
- 智能表通过ng模型设置值时不会触发任何事件
- 将ng模型设置为与变量同名,而不是引用该变量
- 角度防止将模型设置为无效的未定义
- Ember.js{{render}}助手模型设置不正确
- ng 模型设置值为选择
- 骨干.js:有没有办法在创建集合时为集合中的所有模型设置属性
- MVC Javascript基于模型设置文本输入值
- 基于mvc模型设置html复选框复选值
- 如何将骨干模型设置为对象'的子数组
- 将“模型”设置为阵列中的特定对象
- 将数据模型设置为其他数据模型的属性
- angularjs ng模型设置默认值
- 控制器中不更新ng模型设置的值
- 将模型设置在场景的中心,分为三个部分
- 顺序主干模型.设置覆盖模型.改变属性
- 如何将 ng-option 模型设置为对象的值