admin管理员组文章数量:1181401
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:
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.
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:
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.
Share Improve this question asked Apr 15, 2013 at 12:12 streetlightstreetlight 5,96813 gold badges65 silver badges102 bronze badges5 Answers
Reset to default 25First method: use the models
property of your collection:
var myModel
for(var i=0; i<myCollection.length; i++) {
myModel = myCollection.models[i];
}
Second method is with the each
method:
myCollection.each(function(model, index, [context]) {...});
Iterating over every model of a collection eg. as give by backbone.js is
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);
});
http://backbonejs.org/#Collection-Underscore-Methods
I think you should try collection_var.models.each(paintThings)
. That should give you direct access to the models of a collection.
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!
I used underscores map.
docs = Backbone.Collection.extend();
this.subViews = _.map(docs.models, function(doc) {
return new DocView({ model: doc });
}, this);
本文标签: javascriptGetting each model from a Backbone CollectionStack Overflow
版权声明:本文标题:javascript - Getting each model from a Backbone Collection - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738257709a2071676.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论