summaryrefslogtreecommitdiff
path: root/addons/barcodes/static/src/js/barcode_handler_mixin.js
diff options
context:
space:
mode:
authorArthur Maniet <arthurmaniet@me.com>2015-07-10 13:38:01 +0200
committerArthur Maniet <arthurmaniet@me.com>2015-09-01 13:26:02 +0200
commit670d759dbf1c7153c3df17f533972b47d13e84f3 (patch)
treee9d7d33f0c5f34f6c5c1d7f54325d7d71c9f77c3 /addons/barcodes/static/src/js/barcode_handler_mixin.js
parent2df4060c776c7a6a946f2b7a164ff7bbbe5e854a (diff)
[ADD] barcodes: BarcodeHandlerMixin that starts/stops the event handling
Only the barcode handlers in the front view should listen to barcode events.
Diffstat (limited to 'addons/barcodes/static/src/js/barcode_handler_mixin.js')
-rw-r--r--addons/barcodes/static/src/js/barcode_handler_mixin.js47
1 files changed, 47 insertions, 0 deletions
diff --git a/addons/barcodes/static/src/js/barcode_handler_mixin.js b/addons/barcodes/static/src/js/barcode_handler_mixin.js
new file mode 100644
index 00000000000..5b01516788f
--- /dev/null
+++ b/addons/barcodes/static/src/js/barcode_handler_mixin.js
@@ -0,0 +1,47 @@
+odoo.define('barcodes.BarcodeHandlerMixin', function(require) {
+"use strict";
+
+var core = require('web.core');
+var View = require('web.View');
+
+// Mixin implementing the common basis for barcode handlers.
+// The object on which this mixin is applied must also include ParentedMixin. Example :
+// Widget.extend(BarcodeHandlerMixin, { ... });
+// Class.extend(PropertiesMixin, BarcodeHandlerMixin, { ... });
+
+return {
+ init: function() {
+ var self = this;
+ this._super.apply(this, arguments);
+ this.__on_barcode_scanned = function () {
+ self.on_barcode_scanned.apply(self, arguments);
+ };
+ this.start_listening();
+ // Handlers inside a View managed by a ViewManager only listen to barcode events while their view is displayed
+ var view = this.findAncestor(function(ancestor) { return ancestor instanceof View });
+ if (view) {
+ view.on('attached', this, this.start_listening);
+ view.on('detached', this, this.stop_listening);
+ }
+ },
+
+ start_listening: function() {
+ if (! this.is_listening) {
+ core.bus.on('barcode_scanned', this, this.__on_barcode_scanned);
+ this.is_listening = true;
+ }
+ },
+
+ stop_listening: function() {
+ if (this.is_listening) {
+ core.bus.off('barcode_scanned', this, this.__on_barcode_scanned);
+ this.is_listening = false;
+ }
+ },
+
+ on_barcode_scanned: function(barcode) {
+ console.error('A class implementing BarcodeHandlerMixin must redefine method on_barcode_scanned.');
+ },
+};
+
+});