我有一个观点
app.View.FriendRequestButtonView = Backbone.View.extend({
initialize: function() {
this.set(a, b, c);
app.on('foo', this.reject, this);
app.on('boo', this.accept, this);
},
reject: function(){
this.set(false, false, false);
},
accept: function(){
this.set(true, false, false);
},
set: function (a, b, c){
if(!a && b && c){
this.events = {
"click .cancel": "dosomething"
};
this.render();
}
}
........
}
当我正常调用视图时,事件"click .cancel": "dosomething"
可以正常工作,但当我通过foo
或boo
触发时,事件无效。我做错了什么?
答案 0 :(得分:1)
视图的events
在初始化期间被绑定;以后设置他们不会工作。调用delegateEvents手动绑定它们:
If an events hash is not passed directly, uses this.events as the source.
这意味着以下内容应该有效:
this.events = {
"click .cancel": "dosomething"
};
this.delegateEvents();
this.render();