backbone.js - collection.each() 不迭代模型

标签 backbone.js

我正在尝试迭代由集合获取的模型。

我有以下代码:

initialize: function() {
                this.collection = new UserCollection();
                this.collection.fetch();
                this.render();
            },

            renderCollection: function() {
                console.log("rendering collection");
                this.collection.each(function(index,model){
                    console.log("model"); 
                });
                console.log(this.collection);
            },

render: function() {
                this.template = _.template(template, {});
                this.$el.html(this.template);
                // some other stuff
                this.renderCollection();
}

和结果:
rendering collection

d {models: Array[0], length: 0, _byId: Object, constructor: function, model: function…}
_byId: Object
_idAttr: "id"
length: 4
models: Array[4]
0: d
_changing: false
_events: Object
_pending: false
_previousAttributes: Object
attributes: Object
created: "2013-02-13 09:22:42"
id: "1"
modified: "2013-02-13 09:22:42"
role: "admin"
username: "email@gmail.com"
__proto__: Object
changed: Object
cid: "c5"
collection: d
id: "1"
__proto__: e
1: d
2: d
3: d
length: 4
__proto__: Array[0]
__proto__: e
 user_list.js:25

所以 fetch 方法确实有效 - 在对象转储中我可以找到 4 条记录,但迭代集合不起作用......

最佳答案

each收藏上给 model本身作为 argument .

尝试这个:

this.collection.each(function(model){
  console.log(model); 
});

它应该给你 model对于当前迭代。

关于backbone.js - collection.each() 不迭代模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14853997/

相关文章:

grails - 将 Backbone 集合传递给 View

jquery - ASP.NET MVC + Backbone.js,这有什么意义吗? ASP.NET MVC 真的需要吗?

javascript - Backbone 验证将输入添加到验证

javascript - 获取的 JSON 未填充到主干模型中

backbone.js - 从项目 View 到父布局 View 的 Marionette 气泡事件?

javascript - Backbone.js 拦截 AJAX 请求

javascript - 事件不在 subview 中触发

javascript - Backbone.js Model::设置自定义比较

javascript - 主干模型不允许我获取数据

javascript - 将 Backbone.Layout 定义为 require js 模块?