Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
1667 Vistas

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.

Avatar
Descartar
Mejor respuesta

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

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
0
nov 24
1346
2
jun 22
11191
0
jul 19
3090
0
mar 25
563
0
ene 25
726