我有以下代码,我试图为 ApplicationRoute
设置模型,但它似乎不起作用。我对 Ember 代码有一些疑问。首先,我可以设定申请路线的模型吗?其次,如果路由的模型具有名为 count 和 fileName 的字段,我是否还需要在 Controller 中声明这些字段。看起来如果我这样做, Controller 中的值优先于模型值。我也可以在 setupController
中执行类似 this.set('total',5)
的操作,即使 Total 没有在任何地方定义。
App.ApplicationRoute=Ember.Route.extend({
model:function(){
console.log('model called');
return {count:3,fileName:'Doc1'};
},
setupController:function(){
console.log(this.get('model').fileName);
this.set('count',this.get('model.count')); //Do I manually need to do this?
this.set('fileName',this.get('model.fileName')); //Do I manually need to do this?
}
});
App.ApplicationController=Ember.Controller.extend({
count:0,//Is this necessary?? Can I directly set the property with declaring it like this
fileName:''
});
最佳答案
你可以这样做:
App.ApplicationController=Ember.Controller.extend({
count: function(){
return this.get('model').get('count');
}.property('model.count')
});
因此,只要 model.count
发生变化,该属性就会自动更新。
是的,您可以直接在路线上设置模型。当您在 Controller 中执行 this.set('total', 5)
时,您仅在 Controller 上设置该属性,而不是在模型上设置该属性。为了更新模型,您需要执行以下操作:
var model = this.get('model');
model.set('total', 5);
最后,您的 setupController
代码不正确。以下是 Ember 文档 ( located here ) 上找到的示例方法:
App.SongRoute = Ember.Route.extend({
setupController: function(controller, song) {
controller.set('model', song);
}
});
关于javascript - ember.js 中的应用程序路由模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23670543/