I've got a Backbone.Collection
full of models; let's say that model is Car
. This collection is a great, big list of Cars
. I want to be able to have a few specific car IDs selected from a list, and then be able to get just those selected car objects out of this collection.
My code block below isn't working; I'm sure there's a way to do this with Backbone.js/Underscore.js… I'm pretty fresh to Backbone/Underscore, too.
CarList = Backbone.Collection.extend({
model: Car,
filterWithIds: function(ids) {
return this.filter(function(aCar) { return _.contains(ids, car.id); }
}
});
Any pointers?
Okay, I think I've got it. It's close to my original code block, but the updated filterWithIds
function is here.
filterWithIds: function(ids) {
return _(this.models.filter(function(c) { return _.contains(ids, c.id); }));
}
For those following along in CoffeeScript (I am), here's the CoffeeScript version.
filterWithIds: (ids) -> _(@models.filter (c) -> _.contains ids, c.id)
It's my answer; any code smell?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With