1

我有以下代码,我试图为其设置模型,ApplicationRoute但它似乎不起作用。我对 Ember 代码有一些疑问。首先,我可以为申请路线设置一个模型吗?其次,如果路由模型具有名为 count 和 fileName 的字段,我是否还需要在控制器中声明这些字段。看起来如果我这样做,控制器中的值优先于模型值。即使在任何地方都没有定义总数 ,我也可以做类似this.set('total',5)的事情。setupController

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

1 回答 1

1

你可以做:

App.ApplicationController=Ember.Controller.extend({
    count: function(){
       return this.get('model').get('count');
    }.property('model.count')
});

因此,任何时候model.count更改,属性都会自动更新。

是的,您可以直接在路线上设置模型。当您this.set('total', 5)在控制器中执行此操作时,您只需在控制器而不是模型上设置该属性。为了更新模型,您需要执行以下操作:

var model = this.get('model');
model.set('total', 5);

最后,您的setupController代码不正确。这是在 Ember 文档中找到的示例方法(位于此处):

App.SongRoute = Ember.Route.extend({
  setupController: function(controller, song) {
    controller.set('model', song);
  }
});
于 2014-05-15T06:31:28.663 回答