2014-01-30 85 views
6

我有以下的道具在我的控制器灰燼 - 觀察與物業​​衝突

App.TeamController = Ember.ObjectController.extend(
    involvedProjectTeams: (-> 
    return @get("content.projectTeams").filter (projectTeam, index, enumerable) -> 
     projectTeam.get("sdeScopingWeeks") isnt 0 
).property("[email protected]") 
    notInvolvedProjectTeams: (-> 
    return @get("content.projectTeams").filter (projectTeam, index, enumerable) -> 
     return projectTeam.get("sdeScopingWeeks") is 0 
).observes("[email protected]") 
) 

然後我遍歷都involvedProjectTeams以及底層模板notInvolvedProjectTeams。我收到以下錯誤:

Uncaught TypeError: Object function() { 
     return this.get("content.projectTeams").filter(function(projectTeam, index, enumerable) { 
     return projectTeam.get("sdeScopingWeeks") === 0; 
     }); 
    } has no method 'addArrayObserver' 

爲什麼property()按預期工作,但觀察會引發錯誤?

謝謝!

回答

8

從Ember的導遊

In a nutshell, computed properties let you declare functions as properties. You create one by defining a computed property as a function, which Ember will automatically call when you ask for the property. You can then use it the same way you would any normal, static property.

所以,如果你想在模板中訪問的東西,它應該是一個屬性。

觀察者只會返回函數,所以無法在模板中訪問。這就是爲什麼你在訪問notInvolvedProjectTeams時出錯的原因,它只是一個函數而非屬性。

定義這也是一個計算屬性,以便您可以在模板中訪問它們。

P.S:您可以使用ember的reduceComputed來定義這些屬性。