You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo15中如何仅在指定视图执行JavaScript避免全局影响

在Odoo 15中限制JavaScript仅在批次验证视图执行的方案

要解决JS代码全局执行导致的报错和性能问题,核心是只在目标视图的上下文里加载并执行自定义逻辑,以下是几种可靠的实现方式:

一、通过自定义视图渲染器绑定目标视图(推荐,性能最优)

这种方式直接将自定义JS逻辑与批次验证视图绑定,不会影响其他视图的渲染流程。

1. 修改JS文件,封装逻辑并注册专属渲染器

不要全局继承替换FormRenderer,而是创建专属渲染器,仅在初始化时判断当前视图是否为目标视图,再执行逻辑:

odoo.define('your_module.lot_enter', function(require) {
    "use strict";

    const FormRenderer = require('web.FormRenderer');
    const viewRegistry = require('web.view_registry');

    const LotVerificationRenderer = FormRenderer.extend({
        init: function(parent, state, params) {
            this._super.apply(this, arguments);
            // 替换为你的批次验证视图的XML ID(可在视图的record标签中找到)
            const targetViewId = 'your_module.lot_verification_form';
            if (params.viewInfo.viewId !== targetViewId) {
                return; // 非目标视图,跳过自定义逻辑
            }
            // 执行你的初始化逻辑
            this._initLotValidation();
        },

        renderElement: function() {
            this._super.apply(this, arguments);
            const targetViewId = 'your_module.lot_verification_form';
            if (this.state.viewId !== targetViewId) {
                return;
            }
            // 渲染完成后的DOM操作逻辑
            this._postRenderLotActions();
        },

        // 封装原有自定义逻辑到单独方法
        _initLotValidation: function() {
            // 你的原有初始化代码,比如变量声明、事件绑定前置处理
        },

        _postRenderLotActions: function() {
            // 你的DOM操作代码,比如绑定按钮点击、修改字段样式等
        }
    });

    // 仅将该渲染器注册给目标视图,而非全局替换
    viewRegistry.add('lot_verification_renderer', LotVerificationRenderer);
});

2. 修改XML视图,绑定自定义渲染器

在批次验证视图的<form>标签中,通过js_class指定刚才注册的渲染器名称:

<record id="lot_verification_form" model="ir.ui.view">
    <field name="name">stock.lot.verification.form</field>
    <field name="model">stock.lot</field>
    <field name="arch" type="xml">
        <!-- 绑定自定义渲染器 -->
        <form js_class="lot_verification_renderer">
            <sheet>
                <!-- 你的视图字段定义 -->
            </sheet>
        </form>
    </field>
</record>

二、通过视图加载事件判断(兼容场景)

如果不想自定义渲染器,可通过监听Odoo的视图加载事件,判断当前视图后执行逻辑:

odoo.define('your_module.lot_enter', function(require) {
    "use strict";

    const core = require('web.core');
    const ActionManager = require('web.ActionManager');

    ActionManager.include({
        executeAction: function(action, options) {
            const result = this._super.apply(this, arguments);
            // 判断当前动作对应的是否为批次验证视图
            // 可根据视图ID或视图名称判断,替换为你的实际值
            if (action.type === 'ir.actions.act_window' && 
                action.view_id && 
                action.view_id[1] === '批次验证视图') {
                core.bus.once('DOM_updated', this, this._runLotVerificationLogic);
            }
            return result;
        },

        _runLotVerificationLogic: function() {
            // 执行你的自定义逻辑
            // 可额外通过DOM类名二次确认:if (document.querySelector('.lot-verification-view')) {}
        }
    });
});

这种方式需在XML视图的<form>标签中添加自定义类名class="lot-verification-view",方便DOM判断。

关键注意事项

  • 务必使用视图ID或专属类名作为判断依据,避免误匹配其他视图
  • 优先选择自定义渲染器方案,性能更优,不会对其他视图的渲染流程产生额外开销
  • 若使用事件监听,建议用once而非on绑定事件,避免重复执行逻辑

内容的提问来源于stack exchange,提问作者Roberto López

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 09:15:52