How to inherit or override js file in odoo?

2020-07-24 05:55发布

问题:

I want to change a function in js file. How to do it? Is there any ways to override the function?

addons/web/static/src/js/views/form_common.js,

i want to change the function-get_search_result: function(search_val){}

dataset.name_search(search_val, self.build_domain(), 'ilike', 160).done(function(_data) {self._search_create_popup("search", _data);}

need to change the value 160 to something else

Thanks in advance

回答1:

There is a good answer/example on this question, which gives a pretty good overview. That example is actually a bit more in depth than necessary for a global JavaScript change.

If you identify the function you want to override, then it's mostly just a matter of mirroring core and making the override(s) you want. Here's an example overview of how to change the name_search JavaScript behavior:

your_module/manifest.py

...
'data': [
    ...
    'views/assets.xml',
    ...
],
...

your_module/views/assets.xml

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

your_module/static/src/js/custom.js

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

Class = require('web.Class');
mixins = require('web.mixins');

var DataSet = Class.extend(mixins.PropertiesMixin, {
    name_search: function (name, domain, operator, limit) {
        /* Custom code to override/extend core */
    },
});

return {
    DataSet: DataSet,
};

});


回答2:

You can do it like they did in google_calendar:

odoo.define('youmodule.yourmodule', function (require) {
   "use strict";
   var CompletionFieldMixin = require('web.CompletionFieldMixin');

   var _t = core._t;
   var QWeb = core.qweb;

   CompletionFieldMixin.include({
       // You need to redefine the function here
});