arevilla009
arevilla009

Reputation: 453

Override a JS function in Odoo 12

I'm working with Odoo V12 and I need to override a function defined in a JS file and inject some code inside this function for changing a variable value.

The funcion I need to override is this one:

odoo.define('portal.signature_form', function (require){
    "use strict";

    require('web_editor.ready');

    var ajax = require('web.ajax');
    var base = require('web_editor.base');
    var core = require('web.core');
    var Widget = require("web.Widget");
    var rpc = require("web.rpc");

    var qweb = core.qweb;

    var SignatureForm = Widget.extend({
        template: 'portal.portal_signature',
        events: {
            'click #o_portal_sign_clear': 'clearSign',
            'click .o_portal_sign_submit': 'submitSign',
            'init #o_portal_sign_accept': 'initSign',
        },
    // some defined events
        submitSign: function (ev) {
            ev.preventDefault();
            // some code
            return rpc.query({
                route: this.options.callUrl,          <------------------
                params: {
                    'res_id': this.options.resId,
                    'access_token': this.options.accessToken,
                    'partner_name': partner_name,
                    'signature': signature ? signature[1] : false,
                },
            }).then(function (data) {
            // mode code

The objetive is to obtain a custom value and replace it for "route" variable.

I have defined my own JS file for doing it, first of all linking this file in a XML template and then writing my code, but I'm not able to modify the variable commented before. enter image description here

enter image description here

Any suggestion?

Upvotes: 1

Views: 1320

Answers (1)

arryph
arryph

Reputation: 2825

What you need to do is patching the existing SignatureForm widget, instead of inheriting the new widget, which extend function do. Odoo provides way to patch existing JS class, the include function, more details in the official documentation.

Another thing you are getting wrong here is that, route is not a variable, but a property of the object provided to the rpc.query function, and to change that, you have to patch the submitSign function. You can follow this:

signature_form.SignatureForm.include({
  submitSign: function (ev) {
            ev.preventDefault();
            // some code
            return rpc.query({
                route: "your value",          <------------------
                params: {
                    'res_id': this.options.resId,
                    'access_token': this.options.accessToken,
                    'partner_name': partner_name,
                    'signature': signature ? signature[1] : false,
                },
            }).then(function (data) {
            // mode code
})

Assuming that SignatureForm is returned inside an object at the end of the main file.

Upvotes: 1

Related Questions