Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
75
rated 0 times [  82] [ 7]  / answers: 1 / hits: 41310  / 12 Years ago, sun, january 20, 2013, 12:00:00

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.


More From » backbone.js

 Answers
2

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



things.find(function(model) { return model.get('name') === 'Lee'; });

[#80754] Friday, January 18, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hailie

Total Points: 25
Total Questions: 112
Total Answers: 111

Location: Belize
Member since Tue, Dec 8, 2020
4 Years ago
;