copenndthagen
copenndthagen

Reputation: 50750

Backbone.js : Is cid only used for Models

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.

Upvotes: 2

Views: 3283

Answers (2)

jakee
jakee

Reputation: 18566

cid 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 comes 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.

Upvotes: 7

Labib Ismaiel
Labib Ismaiel

Reputation: 1340

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.

Upvotes: 0

Related Questions