2013-02-25 51 views
2

如果我有這樣的燼數據過濾

App.Transaction = DS.Model.extend({ 
    amount: DS.attr('number'), 
    type: DS.attr('string') 
}); 

其中type可以像「VISA」或「萬事達」或「現金」的一些車型。我有一個計算所有交易總額的計算屬性。

totalAmount:function() { 
    return this.getEach('amount').reduce(function(accum, item) { 
     return (Math.round(accum*100) + Math.round(item*100))/100; 
    }, 0); 
}.property('@each') 

我想要做的就是創建一個返回按類型分組的所有交易的總金額計算的另一個屬性(例如,與所有類型的交易總金額==「VISA」)。

我如何在灰燼JS做到這一點?有沒有getAll方法或某種方式來獲取我可以過濾的數組中的所有事務對象?

回答

2

Ember.Array類有filterProperty方法,能爲你做到這一點。您可以致電此特別喜歡:

visaTotalAmount: function() { 
    return this.filterProperty('type', 'VISA').getEach('amount').reduce(function(accum, item) { 
     return (Math.round(accum*100) + Math.round(item*100))/100; 
    }, 0); 
}.property('@each') 

這將過濾掉只是簽證種類和做總的計算像之前。

+0

filterProperty是我需要感謝的方法! – 2013-02-25 03:47:02