mardi 23 juin 2015

How to properly unbind jQuery window event handler in Ember component

I know that the best practice for binding an event handler to a window event with jQuery is

jQuery(window).on('resize', Ember.run.bind(this, this.handleResize));

If you try to unbind the event handler though in willDestroyElement with

$(window).off('resize', this.handleResize);

this doesn't work because .on() and .off() must be called with the exact same reference to the handler function.

But also the following does not work:

$(window).off('resize', Ember.run.bind(this, this.handleResize));

So the only option I can think off to make sure my event handler does not get called after the component was destroyed is something like this:

willDestroyElement: function () {
  this.set('handleResize', null);
}

...as suggested in this stackoverflow question. But this is not really a proper unbinding. Any suggestions?




Aucun commentaire:

Enregistrer un commentaire