admin管理员组文章数量:1421054
In Backbone.js, there is a property I see called "cid"...Is it used only for Model objects (and not view or collection)
Also are "id" and "idAttribute" used by Model objects only? What is the difference? It would be great if you could explain with a very basic example.
In Backbone.js, there is a property I see called "cid"...Is it used only for Model objects (and not view or collection)
Also are "id" and "idAttribute" used by Model objects only? What is the difference? It would be great if you could explain with a very basic example.
Share Improve this question asked Aug 28, 2013 at 11:31 copenndthagencopenndthagen 50.9k105 gold badges313 silver badges492 bronze badges 2- backbonejs/#Model-idAttribute – Stefan Commented Aug 28, 2013 at 11:36
- possible duplicate of backbone.js - id vs idAttribute vs cid – Ram Commented Aug 28, 2013 at 11:38
2 Answers
Reset to default 7cid
is a property for Backbone Models that serves as the unique identifier for each model until they are assigned a real id
. After the model id
or the attribute that matches the idAttribute
has been assigned, the cid
is no longer used. For more information see the backbone.js docs. Also View's have cid
, but that is more for internal bookkeeping and jquery event binding/unbinding.
id
is also a special property for models, it is meant to hold the backend identifier for the model (most databases create some sort of identifier for each new entry/row). When this identifier is labeled id
things work out of the box with Backbone.js, but there are some Databases which label their identifiers differently (for example MongoDB with _id
).
In these cases Backbone doesn't know out-of-the-box to move that property from the attributes to the id
-property. This is where idAttribute
es in handy: You can define it to point to the label of the identifier from the backed (in MongoDB's case _id
) and then Backbone knows to assign the given _id
-attribute to the id
property.
Example:
var noIdModel = new Backbone.Model();
noIdModel.id // this will be undefined
noIdModel.cid // this will be something like c1
var idModel = new Backbone.Model({id: 1});
idModel.id // this will be 1
idModel.cid // this will be something like c2
// extend a model to have an idAttribute
var IdAttributeModel = Backbone.Model.extend({idAttribute: "_id"});
// create and instance of that model
// assign a value for an attribute with the same name as idAttribute
var idAttributeModel = new IdAttributeModel({_id: 1});
idAttributeModel.id // this will be 1
idAttributeModel.cid // this will be something like c3
To really drive the point home:
Each time when Backbone Model's set
is called, it checks if the idAttribute
is present in the attributes to be set and sets that attribute's value to be the new id
. This can be witnessed from this line of code in the Backbone.js source:
if (this.idAttribute in attrs) this.id = attrs[this.idAttribute];
As you can see the default idAttribute is 'id'. Setting your own idAttribute
will result in the Model id
being set accordingly.
from the backbone.js :
A special property of models, the cid or client id is a unique identifier automatically assigned to all models when they're first created. Client ids are handy when the model has not yet been saved to the server, and does not yet have its eventual true id, but already needs to be visible in the UI.
the idAttribute is almost the same but the difference is it contains the id of a model taken from an existing backend.
本文标签: javascriptBackbonejsIs cid only used for ModelsStack Overflow
版权声明:本文标题:javascript - Backbone.js : Is cid only used for Models - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745330348a2653783.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论