Backbone.Collection度日编号型号 [英] Backbone.Collection get model by id
问题描述
我有一个从服务器获取模型的集合。
I have a collection that fetches models from server.
这工作的,现在我要抓住它的ID模型与 MyCollection.at(0)
和获取:
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
如果我试图通过其ID来获取模型我得到:
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
我没有得到 - 文档说清楚,如果给定ID的模式,集合中存在获得()
方法将返回模型。
推荐答案
你有没有设置 Model.idAttribute
在模型?
Have you set Model.idAttribute
on the Model?
var Model = Backbone.Model.extend({
idAttribute:"_id"
});
在默认情况下主干预计id属性被称为ID。当 idAttribute
已定,骨干IDS进行标准化处理,使 model.id
始终可用,即使id属性被称为别的东西。原来的id属性示范的可用的属性
散,并通过 GET
收作方法初探这样。所以:
By default Backbone expects the id property be called id. When the idAttribute
has been set, Backbone standardizes handling of ids so that model.id
is always available, even if the id property is called something else. The original id property is available in the Model's attributes
hash, and as such via the get
methd. So:
model.id === model.get('_id') // -> true
这篇关于Backbone.Collection度日编号型号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!