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