I am trying to search a collection for a model attribute and then grab and return the entire model ?
var myModel = Backbone.Model.extend({
defaults: {
a: '',
b: '',
c: '',
d: '',
e: ''
}
});
My collection has around 100 of myModels.
I am trying to search through the collection by a, find it and then return the entire myModel of a so I can access the other attributes ?
If I understand your question correctly, you want to use the
wheremethod on Backbone collections, here in the docs:http://backbonejs.org/#Collection-where
So, given an instance of MyCollection called myCollection that has MyModels in it, you can say:
and
foundModelswill contain an array of the models you seekBTW, if you are doing a more complex search, use the
filtermethod instead, passing a function as the first argument that returns true on the desired match: