Ember.js {{render}}助手模型未正确设置 [英] Ember.js {{render}} helper model not correctly set

查看:126
本文介绍了Ember.js {{render}}助手模型未正确设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个奇怪的问题。我有一个文章模板。在文章模板中,我调用了一个 {{rendercategory / newcategory}}



新的类别通过一个动作使用错误的模型。当我将它更改为 {{rendercategory / newthis}} 它使用文章模型。



模板

 < script type =text / x-handlebarsdata-template-name =article> 
((...))
{{rendercategory / newcategory}} //调用弹出窗口添加新类别
((...))
< / script>

<! - 弹出式窗口 - >
< script type =text / x-handlebarsdata-template-name =category / new>
< div class =popup>
< h2>添加新类别< / h2>

名称:{{input type =textvalue = name}}< br />
图片:{{view App.UploadFile name =imagefile = image}}< img {{bind-attr src = image}}>< br />
Category-parent:{{input value = categoryRelation}}< br />

< button {{action'saveCategory'}}>保存< / button>
< / div>
< / script>

路由器

  //两个路由都有调用的render,它使用相同的模板
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')
)};

控制器



pre> App.CategoryNewController = Ember.ObjectController.extend({
actions:{
saveCategory:function(){
console.log('类别新控件saveCategory动作'); //被调用
console.log(this.get('model')); //错误的一个
this.get('model')。 //使用{{rendercategory / newcategory}}保存所有类别
}
}
});

请注意,没有类别/新的路由,因为{{render }}助手。请参阅: http://emberjs.com/guides/templates/rendering-with- helpers /#toc_specific (参见页面底部的表格)

解决方案

在文章中,类别是一个 has-many ,所以你设置的模型是 CategoryNewController 类别的数组(除非您在用户单击新的时候省略了创建类别对象的部分或某事。)


I ran in a strange issue. I have an Article template. Within the article template I call a {{render "category/new" category}}.

However when saving the new Category trough an action the wrong model is used. When I change it to {{render "category/new" this}} it uses the Article model. When I leave the model part empty it does also not work.

The Template:

<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>

The Router:

// both routes have the render called, it uses the same template
this.resource('article', {path: '/article/:id'}); 
this.resource('article.new', {path: "/article/new"});

The Model:

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')
)};

The Controller:

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}} 
        }
    }
});

Please note, there is no route for Category/new because it is not needed for the {{render}} helper. See: http://emberjs.com/guides/templates/rendering-with-helpers/#toc_specific (see table at the bottom of the page)

解决方案

On Article, category is a has-many, so you are setting the model of CategoryNewController to an array of Category (unless you've left out a part that creates a Category object when the user clicks new or something.)

这篇关于Ember.js {{render}}助手模型未正确设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆