2013-02-11 37 views
1

我有一個對象列表,通過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'卡等?

+0

注:我試圖使用從http「myFilteredArray'://計算器。com/questions/11502300/how-to-get-index-item-of-array-in-emberjs-view - 我猜我沒有正確使用它。 – jmonteiro 2013-02-11 02:31:55

+0

您正在使用哪個版本的Ember.js?定義計算的屬性不再是Ember.Object.create – Luan 2013-02-11 03:57:52

+0

@Luan:我正在使用1.0.0-pre4。 – jmonteiro 2013-02-13 12:12:21

回答

2

做到這一點的最好辦法是存儲陣列,將持有的顏色過濾掉(或過濾器,如果你願意的話),每一次都有所計算的屬性更新其length通過指定的變化.property('colours.length');

filteredContent: function() { 
    // Triggered every time a colour is added/removed. 
    var colours = this.get('colours'); 
    return this.get('content').filter(function(model) { 
     // Determine whether the current model's colour is in the array of those filtered. 
     return Boolean(jQuery.inArray(model.get('colour'), colours) == -1); 
    }); 
}.property('colours.length') 

然後,我們只需要爲我們的看法能在色彩傳遞給添加到陣列的方式。我們可以用一個不同的函數applyFilters來完成這個工作,它將接受一個參數 - 我們希望排除的顏色。您可以像{{action}}那樣傳遞此顏色:<a {{action "applyFilter" "red"}}>

applyFilter: function(colour) { 
    var colours = this.get('colours'); 
    if (!colour) { 
     // Clear all of the colours if we're clearing the filters. 
     colours.clear(); 
     return; 
    } 

    // Otherwise we can push the new colour into the array, which will trigger 
    // an update of the filteredContent computed property. 
    colours.pushObject(colour); 
} 

完全符合工作的jsfiddle你周圍的混亂:http://jsfiddle.net/9XmqL/

+0

非常感謝你!如果可以的話,我會爲您創建jsfiddle代碼片段提供額外的積分:-) – jmonteiro 2013-02-16 20:14:47

+0

對不起,評論中的格式不正確。請參閱下面的答案。 – Fordi 2013-09-03 14:56:12

1

不幸的是,我不認爲WildHoney的是最好的答案。

添加以下到他們的CSS:

@-webkit-keyframes onUpdate { 
    from { background: yellow; } 
} 
@keyframes onUpdate { 
    from { background: yellow; } 
} 
* { 
    -webkit-animation: onUpdate 1s; 
    animation: onUpdate 1s; 
} 

我可以看到,當內容被更新,整個列表被重繪。對於很長的名單,這可能是一個嚴重的表現。

爲了避免這種情況,您應該只生成一次提供filteredContent的數組 - 當指向內容的指針發生變化時 - 根據對子集的更改以目標方式更新它。

http://jsfiddle.net/wRzRn/2/

+0

這一切都取決於用例。如果您正在生成任何長列表,則需要考慮所有問題。在開發過程中,您應該從最基本的實現開始,然後根據需求進行調整 - 除非開始時的要求是處理大型數據集。 – Wildhoney 2013-09-03 15:36:30

相關問題