Files
odoo_source/addons/barcodes/static/src/js/barcode_handler_mixin.js
T
Arthur Maniet 670d759dbf [ADD] barcodes: BarcodeHandlerMixin that starts/stops the event handling
Only the barcode handlers in the front view should listen to barcode events.
2015-09-01 13:26:02 +02:00

48 lines
1.6 KiB
JavaScript

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.');
},
};
});