我有一個對象列表,通過Ajax從API中獲取。它類似於這樣:如何使用Ember.js顯示(在視圖中)列表子集?
App.Card = Ember.Object.extend({
name: "",
color: ""
});
App.cardsController = Ember.ArrayController.create({
content: [],
createCard: function(data) {
this.pushObject(App.Card.create(data));
},
loadCards: function() {
// Fetch API and use createCard
}
});
當我使用{{#each App.cardsController}}
,我列出所有的卡我的控制器內。
但我想按顏色過濾它們。我如何過濾列表(在Controller內容中)並顯示它們?
我嘗試這樣的做法:
添加代碼裏App.cardsController
:
filterCardsByColor: function() {
array = this.get('content').filter(function(item, index) {
return item.get('color') == 'red';
});
return array;
}.property('[email protected]')
並補充{{#each App.cardsController.filterCardsByColor}}
我的看法。
但我收到以下錯誤,在我的Chrome控制檯:
Uncaught TypeError: Object [object Object] has no method 'addArrayObserver' ember.min.js:18
我做錯了嗎?或者我該怎麼做?我應該將該邏輯移至一個視圖嗎?怎麼樣?我甚至嘗試在Ember.Array.create(array)
內包裝array
,但它沒有解決我的問題。
獎勵:是否可以發送參數到filterCardsByColor,所以我可以要求'red'
卡或'yellow'
卡等?
注:我試圖使用從http「myFilteredArray'://計算器。com/questions/11502300/how-to-get-index-item-of-array-in-emberjs-view - 我猜我沒有正確使用它。 – jmonteiro 2013-02-11 02:31:55
您正在使用哪個版本的Ember.js?定義計算的屬性不再是Ember.Object.create – Luan 2013-02-11 03:57:52
@Luan:我正在使用1.0.0-pre4。 – jmonteiro 2013-02-13 12:12:21