Quantcast
Channel: Ember.JS - Latest topics
Viewing all articles
Browse latest Browse all 4836

Computed property not recalulating on model save()

$
0
0

@therealbenhogan wrote:

Hi,

I have a computed property defined in the application controller such as this:

totalAmount: computed('model.[]', function () {
	return this.get('model').reduce((previousValue, item) => {
		return parseFloat(item.get('amount')) + previousValue;
	}, 0)
}),

The model (item) is as follows with a one to many relationship with categories (validations constant exluded here for brevity):

export default DS.Model.extend(Validations, {
   description: DS.attr('string'),
  date: DS.attr('date'),
  amount: DS.attr('number', {defaultValue: 0}),
  isExpense: DS.attr('boolean', {defaultValue: true}),
 category: DS.belongsTo('category'),

});

and the category model

export default DS.Model.extend({
  name: DS.attr('string'),
  items: DS.hasMany('item')
});

In the controller, there is an addNewItem method which creates the record, validation is performed by ember-cp-validations

There is also a saveItem method which is passed the above record to persist to the database.

While the computed property totalAmount recalculates when a record is destroyed, it’s not updating when I add a new item. I suspect this is because I am saving the category relationship - it was fine before I added this. Have looked at the DS.PromiseObject (not sure if this is the correct approach!), but am struggling to make this work.

Hope you can help!

Many thanks in advance.

Posts: 7

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 4836

Trending Articles