I have an Ember Data model and I'm trying to do a computed property based on properties of an async hasMany relationship. For some reason, it never seems to recompute. How can I do this properly?
The code:
export default DS.Model.extend({
splits: DS.hasMany('split', { async: true }),
amount: Ember.reduceComputed('[email protected]', {
initialValue: 0,
addedItem: function(accValue, split) { return accValue + split.get('amount'); },
removedItem: function(accValue, split) { return accValue - split.get('amount'); }
})
/* Neither of these work either.
amount: Ember.computed.sum('[email protected]') // This doesn't work
amount: Ember.computed('[email protected]', function() {
return this.get('splits').reduce(function(pValue, split) {
return pValue + split.get('amount');
}, 0);
})
*/
});
The failing test (Expected 1350
, Got 0
):
import { test, moduleForModel } from 'ember-qunit';
import Transaction from 'my-app/models/transaction';
moduleForModel('transaction', 'Unit - Transaction Model', {
needs: ['model:split']
});
test('amount', function() {
var transaction = this.subject();
var store = this.store();
transaction.get('splits').addObjects([
store.createRecord('split', { amount: 250 }),
store.createRecord('split', { amount: 1000 })
]);
equal(transaction.get('amount'), 1250);
});