javascript - Getting the sum of a collection (all models) with backbone.js -


i'm learning backbone. have following

window.serverlist = backbone.collection.extend({      model: server,      cputotal: function(){         if (!this.length) return 0;         /*          * not sure how sum them          * this.get('cpu') integer each of collections         */         return this.get('cpu');     }  }); 

i'm calling render method of view this

 window.appview = backbone.view.extend({       // ....       render: function(){          var total_cpu = serverlist.cputotal();          var items = serverlist.length;       }  }); 

the variable total_cpu empty items correct. ideas ?

i know collection working have plenty of items in there, need add cpu's each item in collection page summary.

for know todos example http://documentcloud.github.com/backbone/docs/todos.html have similar setup.

here best way know how:

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

here jsfiddle of solution.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -