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
- assign default value of 0 amount in each model.
- bind
changed
event models of collection, , add/remove collection. - for
changed
event, use reduce method of underscore.js iterate on collection , find sum of amounts.
Comments
Post a Comment