2014-04-03 229 views
0

我有一個觀點骨幹事件

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"作品,但是當我通過fooboo事件不工作觸發它。我做錯了什麼?

回答

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();