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

后端 未结 2 817
心在旅途
心在旅途 2021-01-12 07:20

I\'m just learning backbone. I have the following

window.ServerList = Backbone.Collection.extend({

    model: Server,

    cpuTotal: function(){
        if          


        
2条回答
  •  别那么骄傲
    2021-01-12 07:49

    Here is the best way I know how:

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

    Here is a jsFiddle of the solution.

提交回复
热议问题