我只是在學習骨幹。我有以下使用backbone.js獲取集合(所有模型)的總和
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');
}
});
我從視圖中的渲染方法調用此類似這樣的
window.AppView = Backbone.View.extend({
// ....
render: function(){
var total_cpu = ServerList.cpuTotal();
var items = ServerList.length;
}
});
變量total_cpu始終是空的,但項目是總是正確的。有任何想法嗎 ?
我知道我的集合正在工作,因爲我有很多項目,但是我需要從集合中的每個項目中爲頁面彙總添加所有CPU。
對於那些知道todos示例的人http://documentcloud.github.com/backbone/docs/todos.html我有一個非常類似的設置。
+1的小提琴,併爲那些有興趣在docs:HTTP://documentcloud.github。 COM /下劃線/#降低 –