我只是在学习骨干。我有以下内容
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始终为空,但items始终正确。有任何想法吗 ?
我知道我的集合正在运行,因为其中有很多项目,但我需要将集合中每个项目的所有 CPU 添加到页面摘要中。
对于那些了解 todos 示例的人http://documentcloud.github.com/backbone/docs/todos.html我有一个非常相似的设置。
这是我知道的最好方法:
cpuTotal: function() {
return this.reduce(function(memo, value) { return memo + value.get("cpu") }, 0);
}
这里有一个jsFiddle的解决方案.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)