From 5b01eca5e4ffc2dc453a3a85c9e753236a18d6e7 Mon Sep 17 00:00:00 2001 From: Arthur Maniet Date: Thu, 9 Jul 2015 10:36:35 +0200 Subject: [PATCH] [FIX] barcodes: BarcodeEvents handler binding and unbinding In order to remove an eventhandler, removeEventListener must receive the same function reference that was passed to addEventListener. So the handlers references are kept as attributes of the class. --- addons/barcodes/static/src/js/barcode_events.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/addons/barcodes/static/src/js/barcode_events.js b/addons/barcodes/static/src/js/barcode_events.js index 55347bf458d..d4306532cb9 100644 --- a/addons/barcodes/static/src/js/barcode_events.js +++ b/addons/barcodes/static/src/js/barcode_events.js @@ -14,6 +14,10 @@ var BarcodeEvents = core.Class.extend(mixins.PropertiesMixin, { init: function(parent) { mixins.PropertiesMixin.init.call(this); this.setParent(parent); + // Keep a reference of the handler functions to use when adding and removing event listeners + this.__keydown_handler = _.bind(this.keydown_handler, this); + this.__keyup_handler = _.bind(this.keyup_handler, this); + this.__handler = _.bind(this.handler, this); // Bind event handler once the DOM is loaded // TODO: find a way to be active only when there are listeners on the bus $(this.start.bind(this)); @@ -127,15 +131,15 @@ var BarcodeEvents = core.Class.extend(mixins.PropertiesMixin, { }, start: function(){ - document.body.addEventListener('keydown', this.keydown_handler.bind(this), true); - document.body.addEventListener('keyup', this.keyup_handler.bind(this), true); - document.body.addEventListener('keypress', this.handler.bind(this), true); + document.body.addEventListener('keypress', this.__handler, true); + document.body.addEventListener('keydown', this.__keydown_handler, true); + document.body.addEventListener('keyup', this.__keyup_handler, true); }, stop: function(){ - document.body.removeEventListener('keydown', this.keydown_handler.bind(this), true); - document.body.removeEventListener('keyup', this.keyup_handler.bind(this), true); - document.body.removeEventListener('keypress', this.handler.bind(this), true); + document.body.removeEventListener('keypress', this.__handler, true); + document.body.removeEventListener('keydown', this.__keydown_handler, true); + document.body.removeEventListener('keyup', this.__keyup_handler, true); }, });