mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[IMP] component: give snapshot from 'willPatch' to 'patched'
This commit is contained in:
+10
-5
@@ -140,13 +140,13 @@ developers write components. Here is a complete description of the lifecycle of
|
|||||||
a owl component:
|
a owl component:
|
||||||
|
|
||||||
| Method | Description |
|
| Method | Description |
|
||||||
| --------------------------------------- | --------------------------------------- |
|
| ------------------------------------------------ | --------------------------------------- |
|
||||||
| **[constructor](#constructor)** | constructor |
|
| **[constructor](#constructor)** | constructor |
|
||||||
| **[willStart](#willStart)** | async, before first rendering |
|
| **[willStart](#willStart)** | async, before first rendering |
|
||||||
| **[mounted](#mounted)** | when component is render and in DOM |
|
| **[mounted](#mounted)** | when component is render and in DOM |
|
||||||
| **[willUpdateProps](#willUpdateProps)** | async, before props update |
|
| **[willUpdateProps](#willupdatepropsnextprops)** | async, before props update |
|
||||||
| **[willPatch](#willPatch)** | just before the DOM is patched |
|
| **[willPatch](#willpatch)** | just before the DOM is patched |
|
||||||
| **[patched](#patched)** | just after the DOM is patched |
|
| **[patched](#patchedsnapshot)** | just after the DOM is patched |
|
||||||
| **[willUnmount](#willUnmount)** | just before removing component from DOM |
|
| **[willUnmount](#willUnmount)** | just before removing component from DOM |
|
||||||
|
|
||||||
Note: no hook method should ever be called manually. They are supposed to be
|
Note: no hook method should ever be called manually. They are supposed to be
|
||||||
@@ -248,7 +248,10 @@ Note that modifying the state object is not allowed here. This method is called
|
|||||||
before an actual DOM patch, and is only intended to be used to save some local
|
before an actual DOM patch, and is only intended to be used to save some local
|
||||||
DOM state.
|
DOM state.
|
||||||
|
|
||||||
#### `patched()`
|
The return value of this method will be given as the first argument of the
|
||||||
|
corresponding `patched` call.
|
||||||
|
|
||||||
|
#### `patched(snapshot)`
|
||||||
|
|
||||||
This hook is called whenever a component did actually update its DOM (most
|
This hook is called whenever a component did actually update its DOM (most
|
||||||
likely via a change in its state/props or environment).
|
likely via a change in its state/props or environment).
|
||||||
@@ -257,6 +260,8 @@ This method is not called on the initial render. It is useful to interact
|
|||||||
with the DOM (for example, through an external library) whenever the
|
with the DOM (for example, through an external library) whenever the
|
||||||
component was patched.
|
component was patched.
|
||||||
|
|
||||||
|
The `snapshot` parameter is the result of the previous `willPatch` call.
|
||||||
|
|
||||||
Updating the widget state in this hook is possible, but not encouraged.
|
Updating the widget state in this hook is possible, but not encouraged.
|
||||||
One need to be careful, because updates here will cause rerender, which in
|
One need to be careful, because updates here will cause rerender, which in
|
||||||
turn will cause other calls to patched. So, we need to be particularly
|
turn will cause other calls to patched. So, we need to be particularly
|
||||||
|
|||||||
+8
-4
@@ -160,8 +160,10 @@ export class Component<
|
|||||||
* It is not called on the initial render. This is useful to get some
|
* It is not called on the initial render. This is useful to get some
|
||||||
* information which are in the DOM. For example, the current position of the
|
* information which are in the DOM. For example, the current position of the
|
||||||
* scrollbar
|
* scrollbar
|
||||||
|
*
|
||||||
|
* The return value of willPatch will be given to the patched function.
|
||||||
*/
|
*/
|
||||||
willPatch() {}
|
willPatch(): any {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This hook is called whenever a component did actually update its props,
|
* This hook is called whenever a component did actually update its props,
|
||||||
@@ -175,8 +177,10 @@ export class Component<
|
|||||||
* One need to be careful, because updates here will cause rerender, which in
|
* One need to be careful, because updates here will cause rerender, which in
|
||||||
* turn will cause other calls to updated. So, we need to be particularly
|
* turn will cause other calls to updated. So, we need to be particularly
|
||||||
* careful at avoiding endless cycles.
|
* careful at avoiding endless cycles.
|
||||||
|
*
|
||||||
|
* The snapshot parameter is the result of the call to willPatch.
|
||||||
*/
|
*/
|
||||||
patched() {}
|
patched(snapshot: any) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* willUnmount is a hook that is called each time just before a component is
|
* willUnmount is a hook that is called each time just before a component is
|
||||||
@@ -331,9 +335,9 @@ export class Component<
|
|||||||
_patch(vnode) {
|
_patch(vnode) {
|
||||||
this.__owl__.renderPromise = null;
|
this.__owl__.renderPromise = null;
|
||||||
if (this.__owl__.vnode) {
|
if (this.__owl__.vnode) {
|
||||||
this.willPatch();
|
const snapshot = this.willPatch();
|
||||||
this.__owl__.vnode = patch(this.__owl__.vnode, vnode);
|
this.__owl__.vnode = patch(this.__owl__.vnode, vnode);
|
||||||
this.patched();
|
this.patched(snapshot);
|
||||||
} else {
|
} else {
|
||||||
this.__owl__.vnode = patch(document.createElement(vnode.sel!), vnode);
|
this.__owl__.vnode = patch(document.createElement(vnode.sel!), vnode);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -545,8 +545,10 @@ describe("lifecycle hooks", () => {
|
|||||||
state = { n: 1 };
|
state = { n: 1 };
|
||||||
willPatch() {
|
willPatch() {
|
||||||
steps.push("parent:willPatch");
|
steps.push("parent:willPatch");
|
||||||
|
return 'leffe';
|
||||||
}
|
}
|
||||||
patched() {
|
patched(snapshot) {
|
||||||
|
expect(snapshot).toBe('leffe');
|
||||||
steps.push("parent:patched");
|
steps.push("parent:patched");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user