324*_*423 8 javascript 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');
}
});
Run Code Online (Sandbox Code Playgroud)
我是从像这样的视图的render方法调用它
window.AppView = Backbone.View.extend({
// ....
render: function(){
var total_cpu = ServerList.cpuTotal();
var items = ServerList.length;
}
});
Run Code Online (Sandbox Code Playgroud)
变量total_cpu始终为空,但项始终正确.有任何想法吗 ?
我知道我的收藏正在工作,因为我有很多项目,但我需要从集合中的每个项目中添加所有CPU的页面摘要.
对于那些知道todos示例http://documentcloud.github.com/backbone/docs/todos.html的人我有一个非常相似的设置.
Bri*_*sio 18
这是我知道的最佳方式:
cpuTotal: function() {
return this.reduce(function(memo, value) { return memo + value.get("cpu") }, 0);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7971 次 |
| 最近记录: |