From 3751379f1e9a4c215fb6eb898b4ccc67659b9ace Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Tue, 10 May 2022 21:51:50 +0700 Subject: initial commit 2 --- addons/web/static/src/js/component_extension.js | 42 +++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 addons/web/static/src/js/component_extension.js (limited to 'addons/web/static/src/js/component_extension.js') diff --git a/addons/web/static/src/js/component_extension.js b/addons/web/static/src/js/component_extension.js new file mode 100644 index 00000000..4edaee80 --- /dev/null +++ b/addons/web/static/src/js/component_extension.js @@ -0,0 +1,42 @@ +(function () { + /** + * Symbol used in ComponentWrapper to redirect Owl events to Odoo legacy + * events. + */ + odoo.widgetSymbol = Symbol('widget'); + + /** + * Add a new method to owl Components to ensure that no performed RPC is + * resolved/rejected when the component is destroyed. + */ + owl.Component.prototype.rpc = function () { + return new Promise((resolve, reject) => { + return this.env.services.rpc(...arguments) + .then(result => { + if (this.__owl__.status !== 5 /* not destroyed */) { + resolve(result); + } + }) + .catch(reason => { + if (this.__owl__.status !== 5) /* not destroyed */ { + reject(reason); + } + }); + }); + }; + + /** + * Patch owl.Component.__trigger method to call a hook that adds a listener + * for the triggered event just before triggering it. This is useful if + * there are legacy widgets in the ancestors. In that case, there would be + * a widgetSymbol key in the environment, corresponding to the hook to call + * (see ComponentWrapper). + */ + const originalTrigger = owl.Component.prototype.__trigger; + owl.Component.prototype.__trigger = function (component, evType, payload) { + if (this.env[odoo.widgetSymbol]) { + this.env[odoo.widgetSymbol](evType); + } + originalTrigger.call(this, component, evType, payload); + }; +})(); -- cgit v1.2.3