It is possible to inherit a function and add a code line in JS

Viewed 565

I'm trying to add a code line in a created JS function but I'm not able inject in in the middle of an event trying to inherit the function.

For example, I had this variable: var my_new_var = $(this).val().toString();

This is the allready defined code, where I need to inject my new variable in the params: {} dictionary:

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

    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',
        },
 // init sign and clear sign events

        submitSign: function (ev) {
            ev.preventDefault();

            var self = this;
            var $confirm_btn = self.$el.find('button[type="submit"]');

            var partner_name = self.$("#o_portal_sign_name").val();
            var signature = self.$("#o_portal_signature").jSignature('getData', 'image');
            var is_empty = signature ? this.empty_sign[1] === signature[1] : true;

            this.$('#o_portal_sign_name').parent().toggleClass('o_has_error', !partner_name).find('.form-control, .custom-select').toggleClass('is-invalid', !partner_name);
            this.$('#o_portal_sign_draw').toggleClass('bg-danger text-white', is_empty);
            if (is_empty || ! partner_name) {
                return false;
            }

            $confirm_btn.prepend('<i class="fa fa-spinner fa-spin"></i> ');
            $confirm_btn.attr('disabled', true);

            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,
// ****************** HERE I NEED TO ADD MY NEW LINE **************************
                    'my_new_var': my_new_var,
                },
            }).then(function (data) {
                self.$('.fa-spinner').remove();
                if (data.error) {
                    self.$('.o_portal_sign_error_msg').remove();
                    $confirm_btn.before(qweb.render('portal.portal_signature_error', {message: data.error}));
                    $confirm_btn.attr('disabled', false);
                }
                else if (data.success) {
                    $confirm_btn.remove();
                    var $success = qweb.render("portal.portal_signature_success", {widget: data});
                    self.$('#o_portal_sign_draw').parent().replaceWith($success);
                }
                if (data.force_refresh) {
                    if (data.redirect_url) {
                        window.location = data.redirect_url;
                    } else {
                        window.location.reload();
                    }
                }
            });
        },
...
});

 

Does someone know how to add this new code line?

1 Answers

In order to make your extension works, you need to import your script. To do this, you must extend the XML file where odoo load all JS to be used. Here is an example:

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

To make sure your code is works, try to add a break point ("debugger;" or an alert("debug");) and check if your browser developer tools stop here.

To edit a existing widget method in Odoo you need to import the widget and use the "include" property to edit the existing code. Here an example:

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

    var OdooWidget = require('odoo_module.OdooWidget');

    OdooWidget.include({
        name_widget_method: function () {
            // current code
            // your code
            // current code
            // ...
        }
    });
});

I hope this helps!

Related