[FIX] component: improve unmount/remount behaviour

This commit is contained in:
Géry Debongnie
2019-08-28 10:02:38 +02:00
parent 10cfe0b740
commit e6a3ede4a6
5 changed files with 196 additions and 24 deletions
+10 -9
View File
@@ -278,19 +278,22 @@ export class Component<T extends Env, Props extends {}, State extends {}> {
*
* Note that a component can be mounted an unmounted several times
*/
async mount(target: HTMLElement): Promise<void> {
if (this.__owl__.isMounted) {
async mount(target: HTMLElement, renderBeforeRemount: boolean = false): Promise<void> {
const __owl__ = this.__owl__;
if (__owl__.isMounted) {
return;
}
if (!this.__owl__.vnode) {
// we use the fact that renderId === 1 as a way to determine that the
// component is mounted for the first time
if (!__owl__.vnode) {
const vnode = await this.__prepare();
if (this.__owl__.isDestroyed) {
if (__owl__.isDestroyed) {
// component was destroyed before we get here...
return;
}
this.__patch(vnode);
} else if (renderBeforeRemount) {
const patchQueue = [];
await this.__render(false, patchQueue, undefined, undefined);
this.__applyPatchQueue(<any[]>patchQueue);
}
target.appendChild(this.el!);
@@ -561,9 +564,7 @@ export class Component<T extends Env, Props extends {}, State extends {}> {
const __owl__ = this.__owl__;
const promises: Promise<void>[] = [];
const patch: any[] = [this];
if (__owl__.isMounted) {
patchQueue.push(patch);
}
patchQueue.push(patch);
if (__owl__.observer) {
__owl__.observer.allowMutations = false;
}
+8 -1
View File
@@ -454,7 +454,14 @@ QWeb.addDirective({
ctx.addElse();
// need to update component
const patchQueueCode = async ? `patchQueue${componentID}` : "extra.patchQueue";
let patchQueueCode = async ? `patchQueue${componentID}` : "extra.patchQueue";
if (keepAlive) {
// if we have t-keepalive="1", the component could be unmounted, but then
// we __updateProps is called. This is ok, but we do not want to call
// the willPatch/patched hooks of the component in this case, so we
// disable the patch queue
patchQueueCode = `w${componentID}.__owl__.isMounted ? ${patchQueueCode} : []`;
}
if (QWeb.dev) {
ctx.addLine(`utils.validateProps(w${componentID}.constructor, props${componentID})`);
}