cssinjs icon indicating copy to clipboard operation
cssinjs copied to clipboard

Event listener never removed

Open LinusU opened this issue 9 years ago • 2 comments

You are adding a bound function but then removing the original function, it won't work.

function hello() { console.log('Hello'); }

el.addEventListener('mousein', hello.bind(this));
el.removeEventListener('mousein', hello); // Nothing will be removed

https://github.com/Sitebase/cssinjs/blob/feature-interaction-mixin/interaction-aware-mixin.js

LinusU avatar Apr 22 '15 09:04 LinusU

Thanks for the heads up @LinusU How can this be solved? Do I needs to store the bound version of the function or something?

Sitebase avatar Apr 22 '15 10:04 Sitebase

Yes, the same value that's being passed to addEventListener must be passed to removeEventListener.

If there is a function that will be called when the instance is created (haven't used mixins so I don't know) I would store the bound functions on the new instance. Then just use this.onOver and this.onOut directly.

constructor: function () {
  this.onOver = this.onOver.bind(this);
  this.onOut = this.onOut.bind(this);
}

LinusU avatar Apr 22 '15 11:04 LinusU