Skip to content

Commit

Permalink
Click event handler is now removed correctly. #27
Browse files Browse the repository at this point in the history
`.bind()` is not identical when used more than once. This means `removeEventListener` will not remove the event. Instead a common 'bound' variable needs to be created to handle both the add and remove listener calls.
  • Loading branch information
Thomas Erbe committed Jul 27, 2018
1 parent 816254a commit 0a4250e
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/plugins/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ class Modal {
*/
this.closeButton = this.findCloseButton();

/**
* Create a bound version of our close event handler, this will
* allow us to remove the event listener later on.
*
* @type {Function}
*/
this.boundHandleCloseEvent = this.handleCloseEvent.bind(this);

if(this.closeButton && this.closable ) {
this.setupCloseEvent();
}
Expand Down Expand Up @@ -93,7 +101,7 @@ class Modal {
* @return {undefined}
*/
setupCloseEvent() {
this.closeButton.addEventListener('click', this.handleCloseEvent.bind(this));
this.closeButton.addEventListener('click', this.boundHandleCloseEvent);
}

/**
Expand Down Expand Up @@ -122,7 +130,7 @@ class Modal {
*/
destroy() {
if(this.closable && this.closeButton) {
this.closeButton.removeEventListener('click', this.handleCloseEvent.bind(this));
this.closeButton.removeEventListener('click', this.boundHandleCloseEvent);
}

document.removeEventListener('keyup', this.boundHandleEscapeClose);
Expand Down

0 comments on commit 0a4250e

Please sign in to comment.