Search code examples
javascriptodooodoo-12

How can you overwrite a JavaScript method?


I need to overwrite Javascript Method, and exactky this method :

https://github.com/odoo/odoo/blob/12.0/addons/account/static/src/js/reconciliation/reconciliation_renderer.js#L338-L484

Could anyone explain what should I do to achieve this ?


Solution

  • To override a javascript method by adding creating a new module follow this steps:

    Create a your_js_file_name.js inside static/src/js folder, add it assets_backend templates like this.

    <template id="assets_backend" name="hr assets" inherit_id="web.assets_backend">
            <xpath expr="." position="inside">
                <script type="text/javascript" src="/folder_name/static/src/js/your_js_file_name.js"></script>
            </xpath>
        </template>
    

    In the your_js_file_name.js file define a new module like they did in account module, it's okay to have the same name, but make sure it starts with the folder name (technical name of your module like account, sale, product...etc):

    // just for debugin when you log in Odoo you should see this message
    // if you don't see it this means that the js file is not loaded yet
    // you need to make sure you upgrade the module the xml file that extend the assets_backed is in the manifest.
    console.log('my ReconciliationRenderer is loaded);
    odoo.define('folder_name.ReconciliationRenderer', function (require) {
          "use strict";
    
          var ReconciliationRenderer = require('account.ReconciliationRenderer');
          // you need to require any thing is used inside the code of the method too
          var qweb = core.qweb;  // like Qweb
          // override the method update of LineRenderer class
          ReconciliationRenderer.LineRenderer.include({
            update: function (state) {
                // you can call the original method using this
                this._super(state);
             }
          });
    
    });
    

    for most of Javascript method this should do it. Hope this gives you and idea of what you should do