naive implementation of mounted hook

This commit is contained in:
Géry Debongnie
2019-01-21 14:54:13 +01:00
parent fbf099f1f3
commit cd0ef90075
2 changed files with 21 additions and 3 deletions
+6 -3
View File
@@ -17,7 +17,6 @@ export default class Widget {
name: string = "widget";
template: string = "<div></div>";
vnode: VNode | null = null;
_TEMP: Promise<any>[] | null = null;
parent: Widget | null;
children: Widget[] = [];
@@ -59,6 +58,9 @@ export default class Widget {
if (target) {
target.appendChild(this.el!);
if (document.body.contains(target)) {
this.mounted();
}
}
return vnode;
}
@@ -89,9 +91,10 @@ export default class Widget {
//--------------------------------------------------------------------------
async render(): Promise<VNode> {
this._TEMP = [];
// localized hack to keep track of deferred list
(<any>this)._TEMP = [];
let vnode = this.env!.qweb.render(this.name, this);
await Promise.all(this._TEMP);
await Promise.all((<any>this)._TEMP);
if (!this.el) {
this.el = document.createElement(vnode.sel!);
}
+15
View File
@@ -91,6 +91,21 @@ describe("lifecycle hooks", () => {
await widget.mount(target);
expect(mounted).toBe(false);
});
test("mounted hook is called if mounted in DOM", async () => {
let mounted = false;
class HookWidget extends Widget {
async mounted() {
mounted = true;
}
}
const widget = makeWidget(HookWidget);
const target = document.createElement("div");
document.body.appendChild(target);
await widget.mount(target);
expect(mounted).toBe(true);
target.remove()
});
});
describe("destroy method", () => {