Possible Duplicate:
Backbone.js Empty Array Attribute
I've been working on a backbone project and came to a problem, so I mocked it up simply, but then ran into some things I didn't understand. Here is the code:
(function(){
myModel = Backbone.RelationalModel.extend({
idAttribute: 'id',
relations: [{
type: Backbone.HasMany,
key: 'mysubmodels',
relatedModel: 'mySubmodel',
reverseRelation: {
key: 'mymodel',
includeInJSON: 'id',
},
}],
});
mySubmodel = Backbone.RelationalModel.extend({
idAttribute: 'id',
});
myCollection = Backbone.Collection.extend({
model: myModel,
});
myView = Backbone.View.extend({
initialize: function(){
this.collection.bind('add', this.doSomething);
},
doSomething: function(){
console.log('a model has been added')
},
});
}());
$(document).ready(function(){
var data = {
id:1,
data: 'this is one',
mysubmodel: {
data: 'hello'
}
};
var cltn = new myCollection();
console.log(cltn);
var vw = new myView({collection: cltn});
var mdl = new myModel(data);
cltn.add(mdl);
console.log(cltn);
});
Essentially, the part that is confusing me is that the two 'console.log(cltn)' at the bottom are both registering that a model has been added despite the fact that the model is added after the first log.