Here is some code:
var TopPanel = Backbone.View.extend( {
initialize: function() {
this.listenTo(this.model, "change", this.render);
},
render: {
...
}
});
var MyData = Backbone.Model.extend({
initialize: function() {
},
defaults: {
team: {
level:0,
name: "bla"
}
}
});
var myData = new MyData();
var topPanel = new TopPanel({model: myData});
topPanel.render();
var anotherView = new AnotherView( {model: myData} );
In Another view I am changing the model on button click like this:
var team = this.model.get("team");
team.level = 1;
this.model.set("team", team);
When I press the button in Another View, it changes model as written above, but render function of the topPanel View doesn't invoked. However if I change the code above like: var team = this.model.get("team"); team = 1; this.model.set("team", team);
The render function is invoked.
Does this mean that backbone.js doesn't support listening for changes in complex objects in Model ? If it does, what is a correct way to do it?