【问题标题】:how do I add action properly in odoo js framework如何在 odoo js 框架中正确添加动作
【发布时间】:2019-11-19 11:17:42
【问题描述】:

我正在尝试在 Inventory 模块顶部添加条形码扫描仪(更具体地说,在拣货顶部)。任何时候在拣货视图中扫描条形码时,如果可以找到其条形码,则必须添加完成的产品。我已经为“web.AbstractAction”编写了扩展名(我以出勤模块为例),但无法从选择视图内部访问它。我显然错过了需要指定某些设置的地方,但我不知道在哪里,官方文档也无法提出我的问题。

这是我在这个任务上的所有代码:

static/js/barcode_scanner.js

odoo.define('stock.barcode_scanner', function(require) {
    'use sctrict';

    var AbstractAction = require('web.AbstractAction');
    var core = require('web.core');

    var BarcodeAction = AbstractAction.extend({

        start: function() {
            var self = this;
            core.bus.on('barcode_scanned', this, this._onBarcodeScanned);
            return this._super();
            // .then(function() {
            //     if (self.message_demo_barcodes) {
            //         self.setup_message_demo_barcodes();
            //     }
            // });
        },

        destroy: function () {
            core.bus.off('barcode_scanned', this, this._onBarcodeScanned);
            this._super();
        },

        _onBarcodeScanned: function(barcode) {
            var self = this;
            core.bus.off('barcode_scanned', this, this._onBarcodeScanned);
            this._rpc({
                    model: 'stock.picking',
                    method: 'product_scan',
                    args: [barcode, ],
                })
                .then(function (result) {
                    if (result.action) {
                        self.do_action(result.action);
                    } else if (result.warning) {
                        self.do_warn(result.warning);
                        core.bus.on('barcode_scanned', self, self._onBarcodeScanned);
                    }
                }, function () {
                    core.bus.on('barcode_scanned', self, self._onBarcodeScanned);
                });
        },

    });

    core.action_registry.add('stock_barcode_scanner', BarcodeAction);

    return {
        BarcodeAction: BarcodeAction,
    };
});

models/stock_picking.py

import logging
from odoo import models, api, _


_logger = logging.getLogger(__name__)


class Picking(models.Model):
    _inherit = "stock.picking"
    _description = "Extended Picking"

    @api.model
    def product_scan(self, barcode):
        _logger.info("i'm trying {}".format(barcode))
        return {'warning': _('%(barcode)s scanned') % {'barcode': barcode}}

views/stock_picking_views.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        <record id="stock_barcode_scanner_action" model="ir.actions.client">
            <field name="name">Pickings</field>
            <field name="tag">stock_barcode_scanner</field>
            <field name="target">fullscreen</field>
        </record>
    </data>
</odoo>

views/web_asset_backend_template.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <template id="assets_backend" name="nickel_inventory assets" inherit_id="web.assets_backend">
        <xpath expr="." position="inside">
            <script type="text/javascript" src="/nickel_inventory/static/js/barcode_scanner.js"></script>
        </xpath>
    </template>

</odoo>

__manifest__.py

# -*- coding: utf-8 -*-
{
    'name': "nickel_inventory",

    'summary': """
    """,

    'description': """
    """,

    'author': "",
    'website': "",

    # Categories can be used to filter modules in modules listing
    # Check https://github.com/odoo/odoo/blob/13.0/odoo/addons/base/data/ir_module_category_data.xml
    # for the full list
    'category': 'Sales/Sales',
    'version': '0.1',

    # any module necessary for this one to work correctly
    'depends': ['base', 'product', 'stock'],

    # always loaded
    'data': [
        # 'security/ir.model.access.csv',
        'views/product_views.xml',
        'views/stock_picking_views.xml',
        'views/web_asset_backend_template.xml',
    ],
    # only loaded in demonstration mode
    'demo': [],
    'installable': True,
}

我错过了什么?它真的可以在 odoo 视图之上工作,还是我只能在 js 框架视图中使用它?

【问题讨论】:

    标签: python odoo odoo-13


    【解决方案1】:

    如果有人好奇我是如何解决这个问题的 - 我已经为菜单项添加了绑定。就我而言,odoo 会打开清晰的白页并听取键盘输入。

    views/stock_picking_views.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <odoo>
        <data>
            <record id="stock_barcode_scanner_action" model="ir.actions.client">
                <field name="name">Pickings</field>
                <field name="tag">stock_barcode_scanner</field>
                <field name="target">fullscreen</field>
            </record>
    
            <menuitem id="menu_stock_barcode_scanner" name="Barcode scanner" sequence="20" action="stock_barcode_scanner_action"/>
        </data>
    </odoo>
    
    

    下一步是用有用的表格和逻辑填充这个空白页面。

    【讨论】:

    • 嗨@vladkhard,我的条形码模块中有类似的问题。我正在尝试向新添加到 stock.picking 的 one2many 字段添加新记录。一切似乎都很完美,但是当我尝试“确认”按钮时,它会引发“未捕获的类型错误:无法读取未定义的属性 'res_id'”的 js 错误。如果您有一些想法,请提供帮助..提前感谢
    猜你喜欢
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多