From dcf9a4c38da5d50a6e597d3d68c3d46fdef61df5 Mon Sep 17 00:00:00 2001 From: "Michael Mattiello (mcm)" Date: Mon, 16 Mar 2020 08:28:17 +0000 Subject: [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'); }); --- addons/web/static/src/js/core/utils.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'addons/web/static/src/js/core/utils.js') diff --git a/addons/web/static/src/js/core/utils.js b/addons/web/static/src/js/core/utils.js index f1d4b00c6a9..e8924747616 100644 --- a/addons/web/static/src/js/core/utils.js +++ b/addons/web/static/src/js/core/utils.js @@ -527,7 +527,12 @@ var utils = { /** * Patch a class and return a function that remove the patch * when called. - * + * + * This function is the last resort solution for monkey-patching an + * ES6 Class, for people that do not control the code defining the Class + * to patch (e.g. partners), and when that Class isn't patchable already + * (i.e. when it doesn't have a 'patch' function, defined by the 'web.PatchMixin'). + * * @param {Class} C Class to patch * @param {string} patchName * @param {Object} patch -- cgit v1.2.3