标签:style blog color os io strong ar div cti
change事件:
"change" (model, options) — 当attributes变化时
"change:[attribute]" (model, value, options) — 当attributes的一个特定属性变化时
listenTo事件:
view.listenTo(model, ‘change‘, view.render); 第一个参数是模块,第二个参数是事件类型,第三个参数是事件回调。
change与listenTo的区别是后者改写了this的指向。
事件绑定
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‘); //1 user.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
事件与视图
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‘);
运行结果是body添加了一个span标签:<span>show</span>
标签:style blog color os io strong ar div cti
原文地址:http://www.cnblogs.com/qianlegeqian/p/3935600.html