You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This commit updates the documentation of the patch function
with the changes done in odoo/odoo#125716.
closes#5017
Signed-off-by: Antoine Vandevenne (anv) <anv@odoo.com>
// this is probably the usual case: patching a class method
132
-
patch(MyClass.prototype, "prototype patch", {
122
+
patch(MyClass.prototype, {
133
123
myPrototypeFn() {...},
134
124
});
135
125
@@ -149,9 +139,9 @@ constructor and patch that method instead:
149
139
}
150
140
}
151
141
152
-
patch(MyClass.prototype, "constructor", {
142
+
patch(MyClass.prototype, {
153
143
setup() {
154
-
this._super(...arguments);
144
+
super.setup(...arguments);
155
145
this.doubleNumber=this.number*2;
156
146
},
157
147
});
@@ -165,11 +155,11 @@ Patching a component
165
155
166
156
Components are defined by javascript classes, so all the information above still
167
157
holds. For these reasons, Owl components should use the `setup` method, so they
168
-
can easily be patched as well (see the section on :ref:`best practices<frontend/owl/best_practices>`.
158
+
can easily be patched as well (see the section on :ref:`best practices<frontend/owl/best_practices>`).
169
159
170
160
.. code-block:: javascript
171
161
172
-
patch(MyComponent.prototype, "my patch", {
162
+
patch(MyComponent.prototype, {
173
163
setup() {
174
164
useMyHook();
175
165
},
@@ -178,19 +168,91 @@ can easily be patched as well (see the section on :ref:`best practices<frontend/
178
168
Removing a patch
179
169
================
180
170
181
-
The `patch` function has a counterpart, `unpatch`, also located in `@web/core/utils/patch`.
171
+
The `patch` function returns its counterpart. This is mostly useful for
172
+
testing purposes, when we patch something at the beginning of a test, and
173
+
unpatch it at the end.
182
174
183
-
.. js:function::unpatch(obj, patchName)
175
+
.. code-block:: javascript
184
176
185
-
:param Object obj: object that should be unpatched
186
-
:param string patchName: string describing the patch that should be removed
177
+
constunpatch=patch(object, { ... });
178
+
// test stuff here
179
+
unpatch();
187
180
188
-
Removes an existing patch from an object `obj`. This is mostly useful for
189
-
testing purposes, when we patch something at the beginning of a test, and
190
-
unpatch it at the end.
181
+
Applying the same patch to multiple objects
182
+
===========================================
191
183
192
-
.. code-block:: javascript
184
+
It could happen that one wants to apply the same patch to multiple objects but
185
+
because of the way the `super` keyword works, the `extension` can only be used
186
+
for patching once and cannot be copied/cloned (`check the documentation of the keyword <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super#description>`_).
187
+
A function returning the object used to patch can be used to make it unique.
188
+
189
+
.. code-block:: javascript
190
+
191
+
constobj1= {
192
+
method() {
193
+
doSomething();
194
+
},
195
+
};
196
+
197
+
constobj2= {
198
+
method() {
199
+
doThings();
200
+
},
201
+
};
202
+
203
+
functioncreateExtensionObj() {
204
+
return {
205
+
method() {
206
+
super.method();
207
+
doCommonThings();
208
+
},
209
+
};
210
+
}
211
+
212
+
patch(obj1, createExtensionObj());
213
+
patch(obj2, createExtensionObj());
214
+
215
+
.. warning::
216
+
217
+
If an `extension` is based on another then the two extensions should
218
+
be applied separately. Do not copy/clone an extension.
219
+
220
+
.. code-block:: javascript
193
221
194
-
patch(object, "patch name", { ... });
195
-
// test stuff here
196
-
unpatch(object, "patch name");
222
+
constobject= {
223
+
method1() {
224
+
doSomething();
225
+
},
226
+
method2() {
227
+
doAnotherThing();
228
+
},
229
+
};
230
+
231
+
constext1= {
232
+
method1() {
233
+
super.method1();
234
+
doThings();
235
+
},
236
+
};
237
+
238
+
constinvalid_ext2= {
239
+
...ext1, // this will not work: super will not refer to the correct object in methods coming from ext1
240
+
method2() {
241
+
super.method2();
242
+
doOtherThings();
243
+
},
244
+
};
245
+
246
+
patch(object, invalid_ext2);
247
+
object.method1(); // throws: Uncaught TypeError: (intermediate value).method1 is not a function
248
+
249
+
constvalid_ext2= {
250
+
method2() {
251
+
super.method2();
252
+
doOtherThings();
253
+
},
254
+
};
255
+
256
+
patch(object, ext1); // first patch base extension
0 commit comments