Disable Ember.run.bind related event?
I have a component that needs to react to changes in a viewport.
The naive approach was to just bind a jQuery resize listener, but that could mess up Ember's loop.
The best method is to use Ember.run.bind
This works great, but I am wondering how to cancel such an event after the component is no longer active?
+3
source to share
1 answer
Found it out. Ember.run.bind
doesn't really need an unbind method, you can just cancel the jQuery event.
Sample code:
export default Ember.Component.extend({
_resizeListener: null,
didInsertElement: function(){
// keep a reference to the event listener
this._resizeListener = Ember.run.bind(this, this.preformLayout);
Ember.$(window).on('resize', this._resizeListener);
},
willDestroy: function(){
if(this._resizeListener){
// whenever component gets destroyed, unbind the listener
Ember.$(window).off('resize', this._resizeListener);
}
}
});
+8
source to share