繁体   English   中英

ember.js中的应用程序路由模型

[英]Model for application route in ember.js

我在尝试为ApplicationRoute设置模型的地方有以下代码,但它似乎不起作用。 我对Ember代码有一些疑问。 首先,我可以为应用程序路由设置模型吗? 其次,如果路由模型具有名为count和fileName的字段,我是否还需要在控制器中声明这些字段。 看起来,如果我这样做,则控制器中的值将优先于模型值。 我也可以在setupController执行this.set('total',5)这样的事情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更改, 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);
  }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM