Reputation: 8706
I have a collection of models:
city = new M.City
App.citiesList = new C.CitiesList model: city
App.citiesList.fetch()
How can I access to the model with id=15
for example?
I need something like App.citiesList.find(15).name()
, where name()
is model function
When I try to add function find to the collection it is incorrect.
When I try to iterate over App.citiesList.models
- I see only one model or what it is.. I actually don't know.
Thanks a lot!
Upvotes: 0
Views: 794
Reputation: 434615
If App.citiesList
is a Backbone collection then you'd want to use get
:
get collection.get(id)
Get a model from a collection, specified by id.
So this would get you your model from the collection:
fifteen = App.citiesList.get 15
And if you wanted to call a method on it:
App.citiesList.get(15).name()
You'd probably want to make sure you got something back from App.citiesList.get 15
first though (unless you knew it was there of course). Since you're working in CoffeeScript you could use the existence operator like this:
name = App.citiesList.get(15)?.name()
#----------------------------^
to get 15's name or undefined
in the name
variable.
The find
method on App.citiesList
would be Underscore's find
and that doesn't find an object with a particular ID.
Upvotes: 3