Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
1 ตอบกลับ
2080 มุมมอง

I know how to override any Js class or functions or methods. The question is how can i do it dynamically?

Requirement is to use both existing class/functions and overriden class/functions as per choice in the same odoo database. Let say I add a field (Boolean). When it is unchecked (False), then the override should not be done on class/function and only existing class/function should be used and if checked (True) then overrided class/functions should be used.

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hi,

To dynamically switch between the original and custom JavaScript, extend the original JavaScript class with a custom implementation.

For example:


/** @odoo-module **/

import { registry } from "@web/core/registry";

import { useService } from "@web/core/utils/hooks";

import { OriginalComponent } from "@original/module/path";


// Extend the OriginalComponent to add a custom implementation

class CustomComponent extends OriginalComponent {

    setup() {

        super.setup();

        this.orm = useService("orm"); // Initialize the ORM service for data access

    }


    async someMethod(...args) {

        // Fetch a configuration value from the model to decide which implementation to use

        const [configValue] = await this.orm.read(

            'your.model',     // Model name

            [1],              // Record ID

            ['field_name']    // Field to check for custom usage

        );

       

        if (configValue.use_custom_js) {

            // Custom code block if custom_js is enabled

            console.log("Field is enabled..");

            // Add custom logic here as needed

        } else {

            // Fallback to the original method implementation

            return super.someMethod(...args);

        }

    }

}


// Register your component in the registry to make it available in Odoo

registry.category("views").add("custom_component", CustomComponent);


Hope it helps

อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
0
พ.ย. 24
1784
2
มิ.ย. 22
11426
0
ก.ค. 19
3245
0
มี.ค. 25
808
1
มิ.ย. 25
332