2017-06-19 58 views
-2

我是Backbone的新手,面臨一個問題,我需要在我的集合中計算模型的具體值。如何計算骨幹中特定模型(屬性)的總和

作爲參考,我粘貼了一系列帳戶,從中我需要計算所有模型的總和acquisitionCost,unitsnetGainLoss

即在計算acquisitionCost之後,我應該得到:1900,我們需要注意的一件事是值是以字符串形式顯示,如JSON中所示。

{ 
    "accounts": [{ 
      "accountNumber": "AS10000642", 
      "assets": [{ 
       "assetName": "ABCDEF GHIJKLM", 
       "isin": "GB0008533564", 
       "grossProfit": [{ 
        "acquisitionCost": "500", 
        "units": "10", 
        "netGainLoss": "20" 
       }] 
      }] 
     }, 
     { 
      "accountNumber": "AS10000642", 
      "assets": [{ 
       "assetName": "ABCDEF GHIJKLM", 
       "isin": "GB0008533564", 
       "grossProfit": [{ 
        "acquisitionCost": "1000", 
        "units": "10", 
        "netGainLoss": "20" 
       }] 
      }] 
     }, 
     { 
      "accountNumber": "AS10000407", 
      "assets": [{ 
       "assetName": "ABCDEF GHIJKLM", 
       "isin": "GB0008533564", 
       "grossProfit": [{ 
        "acquisitionCost": "400", 
        "units": "10", 
        "netGainLoss": "20" 
       }] 
      }] 
     } 
    ] 
} 
+1

爲什麼不遍歷集合,並添加了價值?你嘗試過什麼嗎?什麼阻止了你? –

+0

我不知道該怎麼做。因爲我需要計算所有型號的採購成本,單位和netGainLoss的所有值 – user7386493

+1

請做一些基礎研究,如「如何通過骨幹採集循環」和「如何訪問來自骨幹模型的屬性「,併爲解決這個問題做出了誠實的嘗試。請閱讀[問] –

回答

-2

關於骨幹網的一個令人困惑的部分是,一個特定的實現可以用十億種方式完成,但仍然可以是正確的。所以開發人員可以選擇最佳的方法來確保它是模塊化的並且可以擴展以供將來使用。

由於呈現的數據是深嵌套的,因此最好將它們組織到集合或模型的集合和模型中。這樣可以很容易地操作並在其上添加更多功能。您可以將模型視爲小型船舶(資產或利潤)所在的monthership(賬戶)。因此,遵循該結構,給我們這種模式

還需要確保帳號是唯一的,否則,重複將不會被添加到集合。

// Profit Model 
var Profit = Backbone.Model.extend({}); 

// Asset Model 
var Asset = Backbone.Model.extend({ 
    parse: function(resp) { 
    if (resp.grossProfit) { 
     this.profit = new Profit(resp.grossProfit); 

     delete resp.grossProfit; 
    } 

    return resp; 
    } 
}); 

// Assets Collection 
var Assets = Backbone.Collection.extend({ 
    model: Asset 
}); 

// Account Model 
var Account = Backbone.Model.extend({ 
    idAttribute: 'accountNumber', 
    parse: function(resp) { 
    if (resp.assets) { 
     this.assets = new Assets(resp.assets, { 
     parse: true 
     }); 

     delete resp.assets; 
    } 

    return resp; 
    } 
}); 

// Accounts Collections 
var Accounts = Backbone.Collection.extend({ 
    model: Account, 
    getTotals: function() { 
    var acquisitionCost = 0; 
    var units = 0; 
    var netGainLoss = 0; 

    // iterate over the accounts collections 
    this.each(function(account) { 
     // iterate over assets collections for each account 
     account.assets.each(function(asset) { 
     var profit = asset.profit; 
     if (profit) { 
      acquisitionCost += parseInt(profit.get('acquisitionCost'), 10); 
      units += parseInt(profit.get('units'), 10); 
      netGainLoss += parseInt(profit.get('netGainLoss'), 10); 
     } 

     }); 
    }); 

    return { 
     acquisitionCost: acquisitionCost, 
     units: units, 
     netGainLoss: netGainLoss 
    }; 
    } 
}); 

var data = { 
    "accounts": [{ 
    "accountNumber": "AS10000642", 
    "assets": { 
     "assetName": "ABCDEF GHIJKLM", 
     "isin": "GB0008533564", 
     "grossProfit": { 
     "acquisitionCost": "500", 
     "units": "10", 
     "netGainLoss": "20" 
     } 

    } 
    }, { 
    "accountNumber": "AS100006423", 
    "assets": { 
     "assetName": "ABCDEF GHIJKLM", 
     "isin": "GB0008533564", 
     "grossProfit": { 
     "acquisitionCost": "1000", 
     "units": "10", 
     "netGainLoss": "20" 
     } 

    } 
    }, { 
    "accountNumber": "AS10000407", 
    "assets": { 
     "assetName": "ABCDEF GHIJKLM", 
     "isin": "GB0008533564", 
     "grossProfit": { 
     "acquisitionCost": "400", 
     "units": "10", 
     "netGainLoss": "20" 
     } 

    } 
    }] 
}; 

var accounts = new Accounts(data.accounts, { 
    parse: true 
}); 

var getTotalData = accounts.getTotals(); 

console.log("Total acqisition cost - " + getTotalData.acquisitionCost); 
console.log("Total units - " + getTotalData.units); 
console.log("Total net gain loss - " + getTotalData.netGainLoss); 

Check Fiddle

+0

'parse'不應該有副作用,在這種情況下使用'delete'是低效和不必要的。除此之外,在不是那麼大的集合中實例化這樣的模型的嵌套結構會真的減慢瀏覽器的速度(幾乎幾百人就停下來)。 –

+0

@EmileBergeron使用delete的原因是將數據作爲實際帳戶屬性散列的一部分去除,因爲它直接在模型上生存,所以不再需要這些散列。嵌套結構的主要目的是保持它的組織,並在必要時抓住它們。目前,OP的req是微不足道的,計算的數量。但是如果更多的道具被添加了額外的功能呢?如果嵌套的道具繼續存在於'account' attr散列,那麼由於我們繼續玩弄對象和模型數據,所以操縱會變得複雜。 –

+0

是的,我完全理解操作數據,在視圖中使用它等。,一個複雜的結構會很好,但即使如此,我也會採用懶惰的init方式或計算屬性。在我們這裏,它超出了範圍。 –