diff options
Diffstat (limited to 'addons/web/static/src/js/component_extension.js')
| -rw-r--r-- | addons/web/static/src/js/component_extension.js | 42 |
1 files changed, 42 insertions, 0 deletions
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); + }; +})(); |
