-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add destroy method #1
feat: add destroy method #1
Conversation
function _removeEvent (object, type, callback) { | ||
if (object.removeEventListener) { | ||
object.removeEventListener(type, callback, false); | ||
return; | ||
} | ||
object.detachEvent('on' + type, callback); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the reverse of _addEvent
that already exists
_addEvent(targetElement, 'keypress', self._handleKeyEvent); | ||
_addEvent(targetElement, 'keydown', self._handleKeyEvent); | ||
_addEvent(targetElement, 'keyup', self._handleKeyEvent); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is inside mousetrap constructor, I useself._handleKeyEvent
so that I can remove it on destroy
Mousetrap.prototype.destroy = function () { | ||
var self = this | ||
|
||
self.reset() | ||
|
||
_removeEvent(self.target, 'keypress', self._handleKeyEvent); | ||
_removeEvent(self.target, 'keydown', self._handleKeyEvent); | ||
_removeEvent(self.target, 'keyup', self._handleKeyEvent); | ||
|
||
self.target = undefined | ||
self._handleKeyEvent = undefined | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the listeners and clear the instance
This is useful to provide a way to avoid memory leaks
See also ccampbell#428