【问题标题】:Can't loop over backbone collection无法循环主干集合
【发布时间】:2013-05-03 01:48:17
【问题描述】:

似乎我无法遍历主干集合。我在几个线程中看到过这个主题,但这些解决方案都没有帮助。

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;
    }

从这里我的控制台只显示 Array[2] 我也希望看到每个模型。 有谁知道我在这里做错了什么?

【问题讨论】:

    标签: backbone.js underscore.js backbone.js-collections


    【解决方案1】:

    要将您的集合内容作为数组获取,请使用方法toJSON(例如:collection.toJSON()

    接下来,要遍历整个集合,请在集合实例上使用 each 方法!

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

    如果这没有显示您的完整集合,那么问题在于您如何在集合中添加项目,而不是在循环逻辑中。

    【讨论】:

      猜你喜欢
      • 2015-01-31
      • 2014-12-31
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-30
      相关资源
      最近更新 更多