javascript For 循环主干集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11726943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:07:53  来源:igfitidea点击:

For Loop over Backbone Collection

javascriptbackbone.jsmodelcoffeescriptbackbone.js-collections

提问by praks5432

Fairly new to backbone, so this is a really basic question. I have a Backbone collection passed into a function and I can prove that it has been passed and that the models in the collection have ids.

对主干相当陌生,所以这是一个非常基本的问题。我有一个 Backbone 集合传递给一个函数,我可以证明它已经被传递并且集合中的模型有 id。

Here's how I'm setting the ids -

这是我设置 ID 的方式 -

convertToMapObjects: (results)  =>
   objectList = new ObjectList()
   results.each(result)->
    testObj = new TestObject()
    testObj.set
      id = result.get("id")
    objectList.add(testObj)

And in another function ( accessed through making the model trigger an event) -

在另一个函数中(通过使模型触发事件访问)-

getIds: (objects) =>
ids = (object.id for object in objects) 

I think the issue may be because of how I'm iterating through the collection because when I tried doing

我认为这个问题可能是因为我如何遍历集合,因为当我尝试做

for object in objects
   console.log(object)

I saw two undefineds. Is this correct? If so, why can't I use a for loop to go through a backbone collection? Also, is there a way I could do so?

我看到了两个未定义的。这个对吗?如果是这样,为什么我不能使用 for 循环来遍历主干集合?另外,有没有办法做到这一点?

回答by mu is too short

A Backbone collection is not an array so for ... inwon't produce the results you're expecting. You want to look at the collection's modelsproperty if you want to use a simple loop.

Backbone 集合不是数组,因此for ... in不会产生您期望的结果。models如果您想使用一个简单的循环,您需要查看集合的属性。

However, Backbone collections have various Underscore methods mixed in:

但是,Backbone 集合混合了各种 Underscore 方法

Underscore Methods (28)

Backbone proxies to Underscore.js to provide 28 iteration functions on Backbone.Collection. They aren't all documented here, but you can take a look at the Underscore documentation for the full details…

  • forEach (each)
  • ...

下划线方法 (28)

Backbone 代理 Underscore.js 以在Backbone.Collection上提供 28 个迭代函数。它们并未全部记录在此处,但您可以查看 Underscore 文档以获取完整详细信息……

  • forEach(每个)
  • ...

So you can use mapor pluckif you'd like to avoid accessing the modelsproperty:

因此,您可以使用map或者pluck如果您想避免访问该models属性

ids = objects.map (m) -> m.id
ids = objects.pluck 'id'

The pluckmethod is, more or less, just a special case of mapbut collections implement a native version rather than using the Underscore version so that they can pluck model attributes rather than simple object properties.

pluck方法或多或少只是一个特殊情况,map但集合实现了原生版本而不是使用 Underscore 版本,以便它们可以提取模型属性而不是简单的对象属性。

回答by Colin Brock

You want to loop over the modelspropertyof the collection, not the collection object itself.

您想遍历集合的models属性,而不是集合对象本身。

回答by Venkat Kotra

for object in object.models

This will give you a model in the collection

这将为您提供集合中的模型