如何覆盖 JavaScript 方法?

How can you overwrite a JavaScript method?

我需要覆盖 Javascript 方法,并且精确地使用这个方法 :

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

谁能解释一下我应该怎么做才能实现这一目标?

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

static/src/js 文件夹中创建一个 your_js_file_name.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 方法,这应该可以做到。希望这能让你知道你应该做什么