您如何覆盖JavaScript方法?

时间:2019-10-05 00:09:55

标签: javascript odoo odoo-12

我需要覆盖Javascript方法,并严格要求此方法:

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

有人可以解释我该怎么做吗?

1 个答案:

答案 0 :(得分:3)

要通过添加创建新模块来覆盖javascript方法,请按照以下步骤操作:

your_js_file_name.js文件夹中创建一个static/src/js,并将其添加到assets_backend模板中。

<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>

your_js_file_name.js文件中定义一个新模块,就像在account模块中一样,可以使用相同的名称,但请确保它以文件夹名称(technical name您的module,例如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);
         }
      });

});
对于大多数Javascript方法,

应该这样做。希望这能给您和您应该做什么的想法