【问题标题】:Getting each model from a Backbone Collection从主干集合中获取每个模型
【发布时间】:2013-04-15 12:12:12
【问题描述】:

我确信这是一个非常容易解决的问题,但到目前为止我发现的所有帖子似乎都没有直接解决这个问题:我如何遍历集合以获取每个模型?

我尝试使用的第一种方法是下划线的 each 方法。这是我的电话和功能:

collection_var.each(paintThings);

这是我的功能:

function paintThings() {
        console.log(this);
        console.log(this.model);
            var thing_type = this.model.get("type"),
                thing_other = this.model.get("otherAttribute");

                console.log(this.model);
                console.log(thing_type);
                console.log(thing_other);
        }

现在,这是未定义的,并且 this.model 错误:

Uncaught TypeError: Cannot read property 'model' of undefined 

我知道答案很简单,但它让我发疯!我是下划线的新手。这里有人可以帮忙吗?如果它们更快/更好,我也对其他非下划线方法持开放态度。

我也试过这个:

 for (var i = 0, l = collection_var.length; i < l; i++) {
            console.log(collection_var[i]);
 }

但这也不是我想要的。

【问题讨论】:

    标签: javascript backbone.js underscore.js


    【解决方案1】:

    第一种方法:使用集合的models 属性:

    var myModel
    for(var i=0; i<myCollection.length; i++) {
      myModel = myCollection.models[i];
    }
    

    第二种方法是each方法:

    myCollection.each(function(model, index, [context]) {...});
    

    【讨论】:

    • 这太棒了!感谢您发布这两个选项。您的解决方案比我的更优雅,因为它不需要事先定义函数。再次感谢!
    • @streetlight 取决于。在别处声明函数可能会给你更多的结构代码。那只是展示解决方案的懒惰方式;)
    • @Loamhoof 。是否强调每种方法?如果是,那么 _ 符号在哪里。请告诉我 。谢谢
    • @Mahi 几乎所有的下划线方法都可以直接用作 Collections 方法。见backbonejs.org/docs/backbone.html#section-147
    【解决方案2】:

    迭代集合的每个模型,例如。正如backbone.js 给出的那样

    books.each(function(book) {
      book.publish();
    });
    

    在你的情况下应该是这样的

    collection_var.each(function(paintThing){
        console.log(paintThing);
        var thing_type = this.model.get("type"),
            thing_other = this.model.get("otherAttribute");
    
        console.log(paintThing);
        console.log(thing_type);
        console.log(thing_other);
    });
    

    http://backbonejs.org/#Collection-Underscore-Methods

    【讨论】:

      【解决方案3】:

      我认为你应该试试collection_var.models.each(paintThings)。这应该使您可以直接访问集合的模型。

      【讨论】:

      • 不,下划线方法适用于 Backbone 的集合,而不是数组(虽然它可以工作,但这不是它的用途)。
      【解决方案4】:

      好吧,愚蠢的错误!我只需要向函数传递一个额外的参数,这样我就可以触摸模型了。

      像这样:

      function paintThings(thing) {
          //returns model
          console.log(thing);
              var thing_type = thing.get("type"),
                  thing_other = thing.get("otherAttribute");
      
                  //These all respond directly
                  console.log(thing);
                  console.log(thing_type);
                  console.log(thing_other);
          }
      

      我还是会审核所有答案并接受最优雅的解决方案!

      【讨论】:

        【解决方案5】:

        我使用了下划线映射。

        docs = Backbone.Collection.extend();
        
        this.subViews = _.map(docs.models, function(doc) {
            return new DocView({ model: doc });
        }, this);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-02
          • 1970-01-01
          相关资源
          最近更新 更多