javascript 从 Backbone 集合中获取每个模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16014854/
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
Getting each model from a Backbone Collection
提问by streetlight
I'm convinced this is a very easy fix, but none of the posts I've found so far seem to have addressed this directly: how do I loop over a collection to get each model?
我确信这是一个非常简单的解决方法,但到目前为止我发现的所有帖子似乎都没有直接解决这个问题:如何遍历集合以获取每个模型?
The first method I'm trying to use is underscore's each method. Here is my call and function:
我尝试使用的第一种方法是下划线的每个方法。这是我的电话和功能:
collection_var.each(paintThings);
and here is my function:
这是我的功能:
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);
}
Right now, this comes out as undefined, and this.model errors:
现在,这是未定义的,并且 this.model 错误:
Uncaught TypeError: Cannot read property 'model' of undefined
I know the answer is simple, but it's driving me crazy! I'm new to underscore. Can anyone here help? I'm also open to other non-underscore methods if they are faster/better.
我知道答案很简单,但它让我发疯!我是新来的下划线。这里有人可以帮忙吗?如果它们更快/更好,我也愿意接受其他非下划线方法。
I also tried this:
我也试过这个:
for (var i = 0, l = collection_var.length; i < l; i++) {
console.log(collection_var[i]);
}
but that's not giving me what I want either.
但这也没有给我我想要的。
回答by Loamhoof
First method: use the models
property of your collection:
第一种方法:使用models
您的集合的属性:
var myModel
for(var i=0; i<myCollection.length; i++) {
myModel = myCollection.models[i];
}
Second method is with the each
method:
第二种方法是用each
方法:
myCollection.each(function(model, index, [context]) {...});
回答by Venkat Kotra
Iterating over every model of a collection eg. as give by backbone.js is
迭代集合的每个模型,例如。正如backbone.js 给出的那样
books.each(function(book) {
book.publish();
});
In your case it should be something like this
在你的情况下,它应该是这样的
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);
});
回答by Gustav
I used underscores map.
我使用了下划线映射。
docs = Backbone.Collection.extend();
this.subViews = _.map(docs.models, function(doc) {
return new DocView({ model: doc });
}, this);
回答by Codrin Eugeniu
I think you should try collection_var.models.each(paintThings)
. That should give you direct access to the models of a collection.
我觉得你应该试试collection_var.models.each(paintThings)
。这应该可以让您直接访问集合的模型。
回答by streetlight
Okay, stupid error! I just had to pass an extra parameter to the function so I can touch the model.
好吧,愚蠢的错误!我只需要向函数传递一个额外的参数,这样我就可以触摸模型。
Like this:
像这样:
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);
}
I'm still going to review all answers and accept the most elegant solution!
我仍然会查看所有答案并接受最优雅的解决方案!