ember.js - Ember Set/Save Doesn't Update Computed Properties -
i'm working on simple budgeting app 2 models: category , entries. categories have many entries.
ember 2.2
problem: categories have computed properties total
, difference
watch associated entries. these computed properties update when entries added , removed, not when entry attributes change.
here category model:
# app/models/category.js export default ds.model.extend({ name: ds.attr('string'), budgetamount: ds.attr('number'), budgetsheet: ds.belongsto('budget-sheet'), entries: ds.hasmany('entry'), total: ember.computed('entries.@each.amount', { get() { let entries = this.get('entries'); return entries.reduce(function(previousvalue, entry){ return previousvalue + entry.get('amount'); }, 0); } }), difference: ember.computed('budgetamount', 'total', { get() { return this.get('budgetamount') - this.get('total'); } }) });
here route:
# app/routes/budget-sheet.js export default ember.route.extend(authenticatedroutemixin, { model(params) { return this.store.find('budget-sheet', params.budgetsheetid); }, setupcontroller(controller, model) { controller.set('budgetsheet', model); controller.set('categories', model.get('categories')); } });
here controller actions add/remove/update entries:
# app/controllers/budget-sheet.js export default ember.controller.extend({ entries: ember.computed('categories.@each.entries', { get() { let result = []; this.get('categories').map(function(category) { category.get('entries').foreach(function(entry) { return result.push(entry); }); }); return result; } }), actions: { ... updateentry(id, attribute, value) { let entry = this.store.peekrecord('entry', id); entry.set(attribute, value); entry.save(); }, deleteentry(id) { let entry = this.store.peekrecord('entry', id); entry.destroyrecord(); } } });
the template iterates through categories
, displays computed properties. template has entry-form
component responsible sending data functions in controller (addentry
, updateentry
).
when entry added or removed, template updated. when entry's category changed, template updated. when entry's amount updated, template not update.
you did run this open bug @jeffreyj linked.
but work around replacing
total: ember.computed('entries.@each.amount', {
with
total: ember.computed('entries.content.@each.amount', {
this because entries
return promisearray
, there @each
broken. unter content
have actual value, work.
Comments
Post a Comment