diff options
| author | Michael Mattiello (mcm) <mcm@odoo.com> | 2020-03-16 08:28:17 +0000 |
|---|---|---|
| committer | Michael Mattiello (mcm) <mcm@odoo.com> | 2020-04-16 07:19:25 +0000 |
| commit | dcf9a4c38da5d50a6e597d3d68c3d46fdef61df5 (patch) | |
| tree | d17339ec7d9a543deb5937052415fe768d1298d7 /addons/web/views | |
| parent | 5cc42022fc159e71ccd2dde122ddde80dd66e23e (diff) | |
[IMP] web: add es6 class patcher
This commit implements a new way to monkey-patch ES6
classes (for instance, Owl Components).
Usage
-----
odoo.module('module.BaseClass', function (require) {
const patchMixin = require('web.patchMixin');
// Define your class as usual
class BaseClass {
constructor() {
this.foo = 'foo';
}
bar() {
console.log(this.foo);
}
}
// but export it like this to have a "patchable" version
return patchMixin(BaseClass);
});
odoo.module('module.Patch', function (require) {
// imports the "patchable" version
const BaseClass = require('module.BaseClass');
BaseClass.patch('key', T =>
class extends T {
constructor() {
super();
this.foo = 'patched foo';
}
bar() {
super.bar();
console.log('patched bar()');
}
}
);
});
odoo.module('module.instance', function (require) {
// imports the "patchable" version
const BaseClass = require('module.BaseClass');
const obj = new BaseClass();
obj.bar();
// if module.patch is loaded before this module the patch
// will be apply and calling obj.bar() will log:
// patched foo
// patched bar()
// You can unpatch a patchable class
BaseClass.unpatch('key');
});
Diffstat (limited to 'addons/web/views')
| -rw-r--r-- | addons/web/views/webclient_templates.xml | 2 |
1 files changed, 2 insertions, 0 deletions
diff --git a/addons/web/views/webclient_templates.xml b/addons/web/views/webclient_templates.xml index 05059f15d55..275a1ccd59f 100644 --- a/addons/web/views/webclient_templates.xml +++ b/addons/web/views/webclient_templates.xml @@ -144,6 +144,7 @@ <script type="text/javascript" src="/web/static/src/js/core/dom.js"></script> <script type="text/javascript" src="/web/static/src/js/core/local_storage.js"></script> <script type="text/javascript" src="/web/static/src/js/core/mixins.js"></script> + <script type="text/javascript" src="/web/static/src/js/core/patch_mixin.js"></script> <script type="text/javascript" src="/web/static/src/js/core/qweb.js"></script> <script type="text/javascript" src="/web/static/src/js/core/ram_storage.js"></script> <script type="text/javascript" src="/web/static/src/js/core/registry.js"></script> @@ -721,6 +722,7 @@ <script type="text/javascript" src="/web/static/tests/core/data_comparison_utils_tests.js"></script> <script type="text/javascript" src="/web/static/tests/core/math_utils_tests.js"></script> <script type="text/javascript" src="/web/static/tests/core/mixins_tests.js"></script> + <script type="text/javascript" src="/web/static/tests/core/patch_mixin_tests.js"></script> <script type="text/javascript" src="/web/static/tests/core/time_tests.js"></script> <script type="text/javascript" src="/web/static/tests/core/concurrency_tests.js"></script> <script type="text/javascript" src="/web/static/tests/core/util_tests.js"></script> |
