How to bind and unbind mouse events on Marionette

1.5k views Asked by At

I have this event in my view:

    events:
        "click" : "clickContainer"

How can I unbind/bind (able and disable) temporary the click event in the same View?

2

There are 2 answers

0
Avery S. On BEST ANSWER

Another option is to remove the events map and use the manual version of what the events map sets up.

onShow: function() {
  this.enableClick();
},

enableClick: function() {
  $(".clickContainer").on("click", this.onClickContainer);
},

disableClick: function() {
  $(".clickContainer").off("click", this.onClickContainer);
},

onClickContainer: function() {
  // do stuff
}
0
kalley On

I would have a property on the view. Something like this:

var View = Marionette.ItemView.extend({
    initialize: function() {
        this.clickEnabled = true;
    },
    events: {
        'click': 'clickContainer'
    },
    clickContainer: function() {
        if ( this.clickEnabled ) {
           // do stuff
        }
    }
});

then you just change that property when you want to change the state.