如果我有一些像这样的模型
App.Transaction = DS.Model.extend({
amount: DS.attr('number'),
type: DS.attr('string')
});其中类型可以是类似于"VISA“或"Mastercard”或"Cash“的内容。我有一个计算的属性,它计算所有事务的总金额。
totalAmount:function() {
return this.getEach('amount').reduce(function(accum, item) {
return (Math.round(accum*100) + Math.round(item*100))/100;
}, 0);
}.property('@each')我想要做的是创建另一个计算属性,它返回按类型分组的所有事务的总金额(例如,类型为“==”的所有交易的总金额)。
我如何在Ember js中做到这一点?有没有一种getAll方法或某种方法可以获得一个数组中的所有事务对象,以便我进行过滤?
发布于 2013-02-25 10:41:59
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')这将只过滤出签证类型,并像以前一样进行总数计算。
https://stackoverflow.com/questions/15058495
复制相似问题