Reputation: 11663
I'm testing Localstorage of Backbone with the following code. After saving the model, it's supposed to create an id and also a cid that is accessible at Model.cid. It's logging the id in the console (see below), but not the cid. Here's a fiddle that recreates the problem http://jsfiddle.net/mjmitche/haE9K/2/
Can anyone see what I'm doing wrong?
// Model Creation with defaults
var Todo = Backbone.Model.extend({
localStorage: new Backbone.LocalStorage("todos"),
defaults: {
title:'new todo...',
description:'missing...',
done:false
}
});
var myTodo = new Todo({});
console.log("Before save id:"+myTodo.get('id')); // Unique Id from Server
console.log("Before save cid:"+myTodo.cid); // Client side id
myTodo.save();
console.log(myTodo.get('title'));
console.log("After save id:"+myTodo.get('id'));
console.log("After save cid:"+myTodo.cid);
Console results
Before save id:undefined app.js:16
Before save cid:c0 app.js:17
new todo... app.js:20
After save id:99bc7f4c-8837-39f4-91e9-90760d8ee8cd app.js:21
After save cid:c0 app.js:22
Upvotes: 0
Views: 1081
Reputation: 111022
The cid is created when the model is created and never change then. See the docs:
A special property of models, the cid or client id is a unique identifier automatically assigned to all models when they're first created
Lets take a look in the constructing function of Backbone.Model:
var Model = Backbone.Model = function(attributes, options) {
.
.
.
this.cid = _.uniqueId('c');
.
.
.
};
As you can see, the cid is created there using underscores uniqueId method.
Upvotes: 2