简体   繁体   English

Odoo 覆盖 Javascript Class 方法

[英]Odoo Override Javascript Class Method

First off, my apologies - I'm a complete novice when it comes to javascript so this is a bit above my head.首先,我很抱歉 - 当谈到 javascript 时,我是一个完全的新手,所以这有点超出我的想象。 I'm also fairly new to Odoo and have mostly stuck with python and XML customization thus far.我对 Odoo 也很陌生,到目前为止,我大多坚持使用 python 和 XML 自定义。

I'm trying to override a javascript method within a class to replace it completely with my own version.我正在尝试覆盖 class 中的 javascript 方法,以用我自己的版本完全替换它。 From the Odoo documentation ( https://www.odoo.com/documentation/14.0/reference/javascript_reference.html#patching-an-existing-class ) this should be a simple matter of using the.include() method to patch the original class with my new method.从 Odoo 文档( https://www.odoo.com/documentation/14.0/reference/javascript_reference.html#patching-an-existing-class )这应该是使用 .include() 方法修补的简单问题用我的新方法原装 class。 But when I do this I get an error Error while loading mymodule.CustomControlPanelModelExtension: TypeError: ControlPanelModelExtension.include is not a function但是当我这样做Error while loading mymodule.CustomControlPanelModelExtension: TypeError: ControlPanelModelExtension.include is not a function

The original Odoo code that I'm trying to override:我试图覆盖的原始 Odoo 代码:

odoo.define("web/static/src/js/control_panel/control_panel_model_extension.js", function (require) {
  "use strict";
    
  // a bunch of code here ...

  class ControlPanelModelExtension extends ActionModel.Extension {
    // more code here ...

    // this is the method I'm trying to override
    _getAutoCompletionFilterDomain(filter, filterQueryElements) {
      // original method body here
    }

    // more code
  }

  // more code
});

Below is what I came up with based on the documentation but this gives me the error Error while loading mymodule.CustomControlPanelModelExtension: TypeError: ControlPanelModelExtension.include is not a function (this error is reported in browser dev tools console).以下是我根据文档提出的内容,但这给了我错误Error while loading mymodule.CustomControlPanelModelExtension: TypeError: ControlPanelModelExtension.include is not a function (此错误在浏览器开发工具控制台中报告)。

odoo.define('mymodule.CustomControlPanelModelExtension', function(require) {
  "use strict";

  var ControlPanelModelExtension = require('web/static/src/js/control_panel/control_panel_model_extension.js');

  ControlPanelModelExtension.include({

    // override _getAutoCompletionFilterDomain 
    _getAutoCompletionFilterDomain: function(filter, filterQueryElements) {
      // my custom implementation here
    },

  });
});

Any ideas what I'm doing wrong here?有什么想法我在这里做错了吗? I've tried various other things with extends and such but I don't think I want to extend - that won't replace the function in existing instances.我已经尝试过使用扩展等各种其他方法,但我认为我不想扩展 - 这不会取代现有实例中的 function。

The problem here is that the include function is available only for the classes that inherit from OdooClass and in this case the class you are trying to inherit is a native JavaScript class. The problem here is that the include function is available only for the classes that inherit from OdooClass and in this case the class you are trying to inherit is a native JavaScript class.


Then, to add a property or method to a class, the prototype property of the object class must be modified.然后,要向 class 添加属性或方法,必须修改 object class 的原型属性。

odoo.define('mymodule.CustomControlPanelModelExtension', function(require) {
    "use strict";

    const ControlPanelModelExtension = require('web/static/src/js/control_panel/control_panel_model_extension.js');

    function _getAutoCompletionFilterDomain(filter, filterQueryElements) {
        // your custom implementation here
    }

    ControlPanelModelExtension.prototype._getAutoCompletionFilterDomain = _getAutoCompletionFilterDomain;

    return ControlPanelModelExtension;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM