mardi 20 octobre 2015

Ember computed property from model array

I have a model with the property "fees"

fees: DS.hasMany('fee')

and a fee model with the property "amount"

amount: DS.attr('string')

in my controller I want a computed property that adds up all of the fee amounts, so I have this

feeTotal: Ember.computed('model.fees.@each.amount', function() {
  var total = 0;
  this.get('model').get('fees').forEach(function(fee) {
    total += Number(fee.get('amount'));
  });
  return total;
})

This will compute the fee initially, and recompute when a new fee is added, but it doesn't recompute when a fee amount is changed. For example, changing the following input field will update the amount but not the computed total.

{{#each model.fees as |fee|}}
  {{input value=fee.amount type="number" placeholder="Fee Amount"}}
{{/each}}

Am I missing something in the way the computed property is written?




Aucun commentaire:

Enregistrer un commentaire