48

I have a model with several object:

//Model
Friend = Backbone.Model.extend({
    //Create a model to hold friend attribute
    name: null,
}); 

//objects
var f1 = new Friend({ name: "Lee" });
var f2 = new Friend({ name: "David"});
var f3 = new Friend({ name: "Lynn"});

and also, I will add these friends object to a collection:

//Collection
Friends = Backbone.Collection.extend({
    model: Friend,
});

Friends.add(f1);
Friends.add(f2);
Friends.add(f3);

and now I want to get a model according to the name of the Friend. I know that I can add an ID attribute to achieve this. But I think there should have some more simple way to do this.

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Anar
  • 925
  • 2
  • 8
  • 15

4 Answers4

87

For simple attribute based searches you can use Collection#where:

where collection.where(attributes)

Return an array of all the models in a collection that match the passed attributes. Useful for simple cases of filter.

So if friends is your Friends instance, then:

var lees = friends.where({ name: 'Lee' });

There's also Collection#findWhere (a later addition as noted in the comments):

findWhere collection.findWhere(attributes)

Just like where, but directly returns only the first model in the collection that matches the passed attributes.

so if you're only after one then you can say things like:

var lee = friends.findWhere({ name: 'Lee' });
mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • Simplicity at its best! – etrast81 Mar 21 '14 at 09:20
  • 6
    Alternatively using `friends.findWhere({name: "Lee"})` should get you only the first model in the collection that matches (thus effectively saving you an `[0]`), I suspect however that it requires Backbone >1.0.0 – conny Sep 26 '15 at 09:38
  • I always strive to use the tools as they were intended to be used... this is the Backbone way! – Pete Feb 04 '16 at 23:34
65

Backbone collections support the underscorejs find method, so using that should work.

things.find(function(model) { return model.get('name') === 'Lee'; });
Andy Ford
  • 8,410
  • 3
  • 26
  • 36
Jani Hartikainen
  • 42,745
  • 10
  • 68
  • 86
  • so the find function will return the model when the callback function's return value is true? – Anar Jan 20 '13 at 00:59
6

The simplest way is to use "idAttribute" option of Backbone Model to let Backbone know the that you want to use "name" as your Model Id.

 Friend = Backbone.Model.extend({
      //Create a model to hold friend attribute
      name: null,
      idAttribute: 'name'
 });

Now you can directly use Collection.get() method to retrieve a friend using his name. This way Backbone does not loop through all of your Friend models in the Collection but can directly fetch a model based on its "name".

var lee = friends.get('Lee');
coderC
  • 249
  • 3
  • 8
5

You can call findWhere() on Backbone collections, that will return exactly the model you are looking for.

Example:

var lee = friends.findWhere({ name: 'Lee' });
Silver Ringvee
  • 5,037
  • 5
  • 28
  • 46