summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/core
diff options
context:
space:
mode:
authorJoseph Caburnay <jcb@odoo.com>2020-03-06 08:11:36 +0000
committerJoseph Caburnay <jcb@odoo.com>2020-03-06 09:04:20 +0000
commitd6b854306cae245343a7acb9edc4178d245a49fe (patch)
treec339d3778c291cea01339f36d9db1c1ad7ed4a43 /addons/web/static/src/js/core
parented1a39c61826d740e2f34f2d2d9072603021503b (diff)
[IMP] web: allow patching getters
Previous implementation of patch (web.utils) only allows patching methods. This commit allows patching getters as well. closes odoo/odoo#47047 Signed-off-by: Géry Debongnie (ged) <ged@openerp.com>
Diffstat (limited to 'addons/web/static/src/js/core')
-rw-r--r--addons/web/static/src/js/core/utils.js52
1 files changed, 44 insertions, 8 deletions
diff --git a/addons/web/static/src/js/core/utils.js b/addons/web/static/src/js/core/utils.js
index 243f3f51cd0..9bc1a287512 100644
--- a/addons/web/static/src/js/core/utils.js
+++ b/addons/web/static/src/js/core/utils.js
@@ -520,6 +520,10 @@ var utils = {
* Patch a class and return a function that remove the patch
* when called.
*
+ * In the patch object, super methods and getters are accessible thru:
+ * - this._super() - for method
+ * - this._super - for getter
+ *
* @param {Class} C Class to patch
* @param {string} patchName
* @param {Object} patch
@@ -531,7 +535,8 @@ var utils = {
metadata = {
origMethods: {},
patches: {},
- current: []
+ current: [],
+ originalDescriptors: {},
};
patchMap.set(C.prototype, metadata);
}
@@ -544,20 +549,41 @@ var utils = {
metadata.current.push(patchName);
function applyPatch(proto, patch) {
- Object.keys(patch).forEach(function (methodName) {
- const method = patch[methodName];
- if (typeof method === "function") {
- const original = proto[methodName];
- if (!(methodName in metadata.origMethods)) {
- metadata.origMethods[methodName] = original;
+ Object.keys(patch).forEach(function(property) {
+ const protoDescriptor = Object.getOwnPropertyDescriptor(proto, property);
+ const patchDescriptor = Object.getOwnPropertyDescriptor(patch, property);
+ if (!protoDescriptor && patchDescriptor) {
+ if (!(property in metadata.originalDescriptors)) {
+ metadata.originalDescriptors[property] = undefined;
+ }
+ Object.defineProperty(proto, property, patchDescriptor);
+ return;
+ }
+ if (protoDescriptor.value) {
+ const method = patch[property];
+ if (typeof method !== 'function') return;
+ const original = proto[property];
+ if (!(property in metadata.origMethods)) {
+ metadata.origMethods[property] = original;
}
- proto[methodName] = function (...args) {
+ proto[property] = function(...args) {
const previousSuper = this._super;
this._super = original;
const res = method.call(this, ...args);
this._super = previousSuper;
return res;
};
+ } else if (protoDescriptor.get) {
+ if (!(property in metadata.originalDescriptors)) {
+ metadata.originalDescriptors[property] = protoDescriptor;
+ }
+ const patchDescriptor = Object.getOwnPropertyDescriptor(patch, property);
+ Object.defineProperty(proto, property, {
+ get() {
+ this._super = protoDescriptor.get.apply(this);
+ return patchDescriptor.get.apply(this);
+ },
+ });
}
});
}
@@ -735,6 +761,16 @@ var utils = {
proto[k] = metadata.origMethods[k];
}
+ // reset the descriptors
+ for (let k in metadata.originalDescriptors) {
+ const descriptor = metadata.originalDescriptors[k];
+ if (descriptor === undefined) {
+ delete proto[k];
+ } else {
+ Object.defineProperty(proto, k, descriptor);
+ }
+ }
+
// apply other patches
for (let name of metadata.current) {
if (name !== patchName) {