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

philhxc

New Member
I'm just learning backbone. I have the following\[code\]window.ServerList = Backbone.Collection.extend({ model: Server, cpuTotal: function(){ if (!this.length) return 0; /* * NOT SURE HOW TO SUM THEM * this.get('cpu') is an integer for each of the collections */ return this.get('cpu'); }});\[/code\]I'm calling this from the render method of a view like this\[code\] window.AppView = Backbone.View.extend({ // .... render: function(){ var total_cpu = ServerList.cpuTotal(); var items = ServerList.length; } });\[/code\]The variable total_cpu is always empty but items is always correct. Any ideas ?I know my collection is working as I have plenty of items in there, but I need to add up all the CPU's from each item in the collection for the page summary. For those who know the todos example http://documentcloud.github.com/backbone/docs/todos.html I have a very similar setup.
 
Top