gpt4 book ai didi

javascript - Ember.js {{render}} 助手模型设置不正确

转载 作者:行者123 更新时间:2023-11-30 17:45:36 26 4
gpt4 key购买 nike

我遇到了一个奇怪的问题。我有一个文章模板。在文章模板中,我调用了一个 {{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 (见页面底部的表格)

最佳答案

在 Article 上,category 是一个 has-many,因此您将 CategoryNewController 的模型设置为 Category 的数组(除非您遗漏了在用户单击“新建”或其他内容时创建 Category 对象的部分。)

关于javascript - Ember.js {{render}} 助手模型设置不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20217830/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com