I have a collection that fetches models from server.
This works, now I want to grab a model by its id with MyCollection.at(0) and I get:
child
_changes: Array[0]
_changing: false
_currentAttributes: Object
_events: Object
_hasComputed: true
_pending: false
_previousAttributes: Object
attributes: Object
_id: "50ef7a63b2a53d17fe000001"
author_name: "author name"
bookmark: ""
info: "bookmark description"
__proto__: Object
changed: Object
cid: "c26"
collection: child
view: child
__proto__: Surrogate
If I try to get the model by its id i get:
MyCollection.get("50ef7a63b2a53d17fe000001")
=> undefined
MyColleciton.get({_id:"50ef7a63b2a53d17fe000001"})
=> undefined
MyCollection.get({'_id':"50ef7a63b2a53d17fe000001"})
=> undefined
I don’t get that – the docs say clearly that the .get() method will return the model if a model with the given id exists in that collection.
Have you set
Model.idAttributeon the Model?By default Backbone expects the id property be called id. When the
idAttributehas been set, Backbone standardizes handling of ids so thatmodel.idis always available, even if the id property is called something else. The original id property is available in the Model’sattributeshash, and as such via thegetmethd. So: