Can't loop over backbone collection

强颜欢笑 提交于 2019-12-11 01:56:03

问题


It seems that I can't loop through a backbone collection. I've seen this topic in several threads, but none of those solutions help.

render:function () {
        this.$el.html(this.template(this.model.attributes));
        var that = this;


        console.log(this.projects.models);
        _.each(this.projects.models, function(model) { console.log(model); });

        return this;
    }

From this my console only shows Array[2] I would expect to see each model as well. Does anyone know what I'm doing wrong here ?


回答1:


To get your collection content as an array, use the method toJSON (e.g.: collection.toJSON())

Next, to loop over your entire collection, use the each method on the collection instances!

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

If this don't show your full collection, then the problem is in how you add items inside your collection, not in the looping logic.



来源:https://stackoverflow.com/questions/16350374/cant-loop-over-backbone-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!