mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
naive implementation of mounted hook
This commit is contained in:
+6
-3
@@ -17,7 +17,6 @@ export default class Widget {
|
|||||||
name: string = "widget";
|
name: string = "widget";
|
||||||
template: string = "<div></div>";
|
template: string = "<div></div>";
|
||||||
vnode: VNode | null = null;
|
vnode: VNode | null = null;
|
||||||
_TEMP: Promise<any>[] | null = null;
|
|
||||||
|
|
||||||
parent: Widget | null;
|
parent: Widget | null;
|
||||||
children: Widget[] = [];
|
children: Widget[] = [];
|
||||||
@@ -59,6 +58,9 @@ export default class Widget {
|
|||||||
|
|
||||||
if (target) {
|
if (target) {
|
||||||
target.appendChild(this.el!);
|
target.appendChild(this.el!);
|
||||||
|
if (document.body.contains(target)) {
|
||||||
|
this.mounted();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return vnode;
|
return vnode;
|
||||||
}
|
}
|
||||||
@@ -89,9 +91,10 @@ export default class Widget {
|
|||||||
//--------------------------------------------------------------------------
|
//--------------------------------------------------------------------------
|
||||||
|
|
||||||
async render(): Promise<VNode> {
|
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);
|
let vnode = this.env!.qweb.render(this.name, this);
|
||||||
await Promise.all(this._TEMP);
|
await Promise.all((<any>this)._TEMP);
|
||||||
if (!this.el) {
|
if (!this.el) {
|
||||||
this.el = document.createElement(vnode.sel!);
|
this.el = document.createElement(vnode.sel!);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -91,6 +91,21 @@ describe("lifecycle hooks", () => {
|
|||||||
await widget.mount(target);
|
await widget.mount(target);
|
||||||
expect(mounted).toBe(false);
|
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", () => {
|
describe("destroy method", () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user