Erik
Erik

Reputation: 14750

How to remove event one handler from view?

In the following code:

var AppView = Backbone.View.extend({

   events:{
      "click .button":"cancel"
   },

   cancel:function() {
      console.log("do something...");
   },

   onSomeEvent: function() {
     this.$el.undelegate('.button', 'click', this.cancel);   
   }
});
var view = new AppView();

I need to undelegate this.cancel handler from elements with 'button' classes. Unfortunately this.$el.undelegate in onSomeEvent method doesn't work.

How could I remove that event handler?

Upvotes: 2

Views: 65

Answers (1)

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

try something like:

....
onSomeEvent: function() {
    this.delegateEvents(
        _(this.events).omit('click .button')
    );    
}

update:

do you mean like:

this.events[event] = "someEvent";
//call delegateEvents() on the view to re-bind events
this.delegateEvents();

Upvotes: 3

Related Questions