Change event:
"Change" (model, options)-When attributes changes
"Change: [attribute]" (model, value, options)-when a specific attribute of attributes changes
LISTENTO event:
View. LISTENTO (model, 'change', view. Render); the first parameter is the module, the second parameter is the event type, and the third parameter is the Event Callback.
The difference between change and LISTENTO is that the latter modifies the point of this.
Event binding
var User=Backbone.Model.extend({ defaults:{ name:‘susan‘, age:18}, initialize:function(){ this.on(‘change‘,function(){ console.log(1); }) }});var user=new User();user.set(‘name‘,‘jack‘);//1user.set(‘name‘,‘lily‘);//1
var User=Backbone.Model.extend({ defaults:{ name:‘susan‘, age:18}, initialize:function(){ this.on(‘change:name‘,function(model){ console.log(model.cid); }) }});var user=new User();user.set(‘name‘,‘lucy‘);//c1
var User=Backbone.Model.extend({ defaults:{ name:‘susan‘, age:18}, initialize:function(){ this.on(‘change:name‘,this.show); }, show:function(){ console.log(‘show‘); }});var user=new User();user.set(‘name‘,‘lucy‘);//show
Events and views
var User=Backbone.Model.extend({ defaults:{ name:‘susan‘, age:18}});var UserView=Backbone.View.extend({ tagName:‘span‘, initialize:function(){ this.listenTo(this.model,‘change:name‘,this.show); }, show:function(){ this.$el.text(‘show‘).appendTo(‘body‘); }});var user=new User();var userview=new UserView({model:user,});user.set(‘name‘,‘lucy‘);
The running result is that a span tag is added to the body: <span> show </span>
Backbone learning records (4)