[ADD] barcodes: BarcodeHandlerMixin that starts/stops the event handling

Only the barcode handlers in the front view should listen to barcode events.
This commit is contained in:
Arthur Maniet
2015-09-01 13:26:02 +02:00
parent 2df4060c77
commit 670d759dbf
3 changed files with 50 additions and 3 deletions
@@ -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.');
},
};
});
@@ -4,15 +4,14 @@ odoo.define('barcodes.FormViewBarcodeHandler', function(require) {
var core = require('web.core');
var utils = require('web.utils');
var common = require('web.form_common');
var BarcodeHandlerMixin = require('barcodes.BarcodeHandlerMixin');
var _t = core._t;
var FormViewBarcodeHandler = common.AbstractField.extend({
var FormViewBarcodeHandler = common.AbstractField.extend(BarcodeHandlerMixin, {
start: function() {
this._super();
this.form_view = this.field_manager;
core.bus.on('barcode_scanned', this, this.on_barcode_scanned);
// Hardcoded barcode actions
this.map_barcode_method = {
'O-CMD.NEW': _.bind(this.form_view.on_button_new, this.form_view),
+1
View File
@@ -6,6 +6,7 @@
<xpath expr="." position="inside">
<script type="text/javascript" src="/barcodes/static/src/js/barcode_parser.js"></script>
<script type="text/javascript" src="/barcodes/static/src/js/barcode_events.js"></script>
<script type="text/javascript" src="/barcodes/static/src/js/barcode_handler_mixin.js"></script>
<script type="text/javascript" src="/barcodes/static/src/js/form_view_barcode_handler.js"></script>
</xpath>
</template>