我有一個在Ember控制器上定義的計算屬性myArray
,該控制器返回一個數組。該陣列應初始化爲model
的內容,然後根據用戶輸入的查詢過濾model
重新進行計算。如何初始化控制器的計算屬性以在Ember中建模?
myArray: function() {
// return a value that filters model with query
}.property('model', 'query')
的問題是,我無法弄清楚如何在同一時間兩者都做。以下不能將myArray
初始化爲model
;我猜測是因爲模型異步加載,並且init()
在它完成之前運行。
// doesn't work
init: function() {
this._super();
this.set('myArray', this.get('model'));
}
所以我認爲setupController()
是設置它的地方,但我發現,設置myArray
有導致過濾器更新不工作,也許是因爲我被覆蓋的定義。
// route definition...
setupController: function(controller, model) {
controller.set('model', model);
controller.set('myArray', model); // breaks updating
}
我該如何實現我的目標?
設置該值不會覆蓋計算出的屬性,它會像調用者那樣調用計算的屬性,如[這裏]所述(http://guides.emberjs.com/v1.11.0/object-model/computed-properties /)。 – GJK
我的不好。感謝您指出。我改變了我的答案以反映這一點。 – jnfingerle