open-nomad/ui/app/utils/properties/sum-aggregation.js
Alex Dadgar e5ec915ac3 sync
2017-09-19 10:08:23 -05:00

15 lines
461 B
JavaScript

import Ember from 'ember';
const { computed } = Ember;
// An Ember.Computed property for summating all properties from a
// set of objects.
//
// ex. list: [ { foo: 1 }, { foo: 3 } ]
// sum: sumAggregationProperty('list', 'foo') // 4
export default function sumAggregationProperty(listKey, propKey) {
return computed(`${listKey}.@each.${propKey}`, function() {
return this.get(listKey).mapBy(propKey).reduce((sum, count) => sum + count, 0);
});
}