javascript - How do I find the total of a particular model attribute in a collection? -


how find total of particular attribute in models of collection?

var mymodel = backbone.model.extend({ });  var mycollection = backbone.collection.extend({     model :  mymodel, }); 

whenever there value change in models, have show total of values.

for example, have 10 models in collection , there property in models called amount. should show total of amount whenever property gets new value.

the code used :

debitamounttotal: function(){     return this.reduce(function(memo, value) {         return memo + value.get("amounttcy");     }, 0); } 

but concatenates values : if first value 8 , second value 8 result 88 instead of 16

  1. assign default value of 0 amount in each model.
  2. bind changed event models of collection, , add/remove collection.
  3. for changed event, use reduce method of underscore.js iterate on collection , find sum of amounts.

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -