1
我有一個控制器是一個ArrayProxy幷包含一些計算數組。如何遍歷在一個計算的Ember陣列在車把視圖中的每個項
在CoffeeScript中,我的控制器看起來像這樣:
MyApp.parentController = Ember.ArrayProxy.create
content: []
mothers: (->
mothers = (person for person in @get("content") when person.sex == 0)
).property("content")
fathers: (->
fathers = (person for person in @get("content") when person.sex == 1)
).property("content")
這工作得很好,當我要求「MyApp.parentController.get(‘母親’)」命令行,我回去預期陣列。
現在,我希望我的文檔中顯示的是一套,所以我想遍歷母親的清單:
<div id="parent-mothers-pool">
{{#each MyApp.parentController.mothers}}
{{#view MyApp.ParentView contentBinding="this"}}{{/view}}
{{/each}}
</div>
這是不行的,而實際上完全掛起的應用,永遠紡紗和甚至沒有將任何錯誤輸出到控制檯。
如您所料,將#each手柄欄中的「.mother」關閉正確顯示所有父母。但把它放在木it上吧。
如何迭代這個計算數組?在模板
MyApp.EnclosingView = Ember.View.extend
mothersBinding: 'MyApp.parentController.mothers'
然後:
我會考慮這一點。與此同時,我計算出迭代計算屬性的答案:首先,必須緩存屬性**,方法是添加.cacheable()。這阻止了應用程序掛起(並且在文檔中沒有提及,natch ...)。其次,在屬性被標記爲可緩存之後,它將實際上停止工作,因爲該屬性應該觀察數組中的每個項目。 因此,計算屬性的固定語法是'母親:(...)。property(「content。@ each」)。cacheable()'。這解決了問題,視圖正常工作。 – 2012-04-04 14:11:52
所以,我的上面的答案放在一邊,是更典型的模式,你必須有自定義視圖的任何任意集合或分組,你可能會選擇有?我覺得我開始疊加大量的三行視圖... – 2012-04-04 14:18:25
在這個答覆中,假設你的封閉視圖是你在這個片段中包含的div後的名字。顯然,'母親'綁定應該被視爲在每個範圍級別上。你必須有一個包含這個div的視圖。 – 2012-04-04 14:31:18