2014-09-05 58 views
0

所以我有一個要求,應該有一個面板顯示用戶未提交的小部件。因爲它必須在每一個觀點,我提出到接受有約束力的,以未提交小部件的計數的分量:Ember綁定控制器屬性在另一個控制器中查詢

# components/unsubmitted-widgets.emblem 
.well.text-center 
    a href="#" My Widgets (#{unsubmittedWidgetsCount}) 
    button.small-button type="button" click="submitWidgets" 
     strong Submit Widgets 

我在想,對於小部件API查詢將進入應用控制器,它所有其他控制器可以綁定到

App.ApplicationController = Ember.Controller.extend 
    unsubmittedWidgets: (-> 
    @store.find('unsubmittedWidget', user: @get('currentUser')) 
).property() 

App.HomeController = Ember.Controller.extend 
    needs: ["application"] 
    unsubmittedWidgetCount: (-> 
    @get('controllers.application.unsubmittedWidgets').toArray().length 
).property('controllers.application.unsubmittedWidgets') 

因此,這引發了請求,我得到了一個結果。但是該視圖不會自動更新。該視圖顯示了我在任何屏幕上的我的Widgets(),當我轉換到另一個視圖存在的路線時,我獲得了真實的值,但是當我回到原始頁面時,它仍然不顯示所有內容。

我該如何將頁面上的值正確地綁定到返回的記錄集的長度?

+2

我想你應該在你的財產上使用['@ each'](http://emberjs.com/guides/object-model/computed-properties-and-aggregate-data/)。 – MilkyWayJoe 2014-09-05 19:45:11

+0

就是這樣。回答它,我會upvote和接受! – DVG 2014-09-08 17:51:08

回答

2

當你創建一個具有集合作爲依賴的屬性,你必須使用@each,就像這樣:

App.HeroesController = Ember.Controller.extend({ 
    identities: [ 
    Em.Object.create({ mask: 'Captain America', name: 'Steve Rogers', isAvenger: true }), 
    Em.Object.create({ mask: 'Iron Man', name: 'Tony Stark', isAvenger: true }), 
    Em.Object.create({ mask: 'Batman', name: 'Bruce Wayne', isAvenger: false }), 
    ], 
    justiceLeague: function() { 
    var identities = this.get('identities'); 
    return identities.filterBy('isAvenger', false).get('length'); 
    }.property('[email protected]'), 
    avengers: function() { 
    var identities = this.get('identities'); 
    return identities.filterBy('isAvenger', true).get('length'); 
    }.property('[email protected]') 
}); 

@each將運行計算資源代碼才能獲得匹配項的個數,只要一個或identities數組中的更多對象獲取isAvenger屬性更新。對於這個例子,它應該顯示兩個字符的計數,考慮三個項目中有一個將過濾的屬性設置爲false。另一個列表觀察完全相同的路徑,但「公式」不同,輸出上面示例的1個字符的計數。

相關問題