2013-02-25 145 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