diff --git a/web/static/src/ts/core/Widget.ts b/web/static/src/ts/core/Widget.ts index 5c12040e..27183bb8 100644 --- a/web/static/src/ts/core/Widget.ts +++ b/web/static/src/ts/core/Widget.ts @@ -51,8 +51,6 @@ export class Widget { async mount(target?: HTMLElement): Promise { await this.willStart(); this.isStarted = true; - this.env.qweb.addTemplate(this.name, this.template); - delete this.template; const vnode = await this.render(); if (target) { @@ -96,6 +94,10 @@ export class Widget { } private async _render(): Promise { + if (this.template) { + this.env.qweb.addTemplate(this.name, this.template); + delete this.template; + } const promises: Promise[] = []; let vnode = this.env.qweb.render(this.name, this, { promises }); return Promise.all(promises).then(() => vnode); diff --git a/web/static/src/ts/core/qweb_vdom.ts b/web/static/src/ts/core/qweb_vdom.ts index d6637194..7239f989 100644 --- a/web/static/src/ts/core/qweb_vdom.ts +++ b/web/static/src/ts/core/qweb_vdom.ts @@ -684,7 +684,7 @@ const widgetDirective: Directive = { `let _${widgetID} = new context.widgets['${value}'](context, ${props})` ); ctx.addLine( - `let def${defID} = _${widgetID}.mount().then(vnode=>Object.assign(_${dummyID}, vnode))` + `let def${defID} = _${widgetID}._render().then(vnode=>{Object.assign(_${dummyID}, vnode);_${dummyID}.data.hook = {create(_,vn){_${widgetID}.el=vn.elm}}})` ); ctx.addLine(`extra.promises.push(def${defID})`); diff --git a/web/static/src/ts/widgets/Counter.ts b/web/static/src/ts/widgets/Counter.ts index 6a426887..ffe27a13 100644 --- a/web/static/src/ts/widgets/Counter.ts +++ b/web/static/src/ts/widgets/Counter.ts @@ -22,7 +22,7 @@ export class Counter extends Widget { } mounted() { - debugger; + console.log("counter mounter", this.el); } increment(delta: number) { this.updateState({ counter: this.state.counter + delta }); diff --git a/web/static/src/ts/widgets/Navbar.ts b/web/static/src/ts/widgets/Navbar.ts index 081f5b03..fd85319c 100644 --- a/web/static/src/ts/widgets/Navbar.ts +++ b/web/static/src/ts/widgets/Navbar.ts @@ -18,6 +18,9 @@ export class Navbar extends Widget { name = "navbar"; template = template; + mounted() { + console.log("navbar mounted", this.el); + } getUrl(menu: Menu) { const action_id = String(menu.actionID); return this.env.router.formatURL("", { action_id }); diff --git a/web/static/src/ts/widgets/clock.ts b/web/static/src/ts/widgets/clock.ts index d159c71d..2b38cf43 100644 --- a/web/static/src/ts/widgets/clock.ts +++ b/web/static/src/ts/widgets/clock.ts @@ -15,6 +15,7 @@ export class Clock extends Widget { } mounted() { + console.log("clock mounter", this.el); setInterval(this.updateTime.bind(this), 500); } diff --git a/web/static/tests/widget.test.ts b/web/static/tests/widget.test.ts index 6de86f9a..a794729f 100644 --- a/web/static/tests/widget.test.ts +++ b/web/static/tests/widget.test.ts @@ -131,20 +131,21 @@ describe("lifecycle hooks", () => { }); test("mounted hook is called on subwidgets, in proper order", async () => { - expect.assertions(2); + expect.assertions(4); let parentMounted = false; let childMounted = false; class ParentWidget extends Widget { name = "a"; template = `
Hello
`; widgets = { child: ChildWidget }; - async mounted() { + mounted() { expect(childMounted).toBe(false); parentMounted = true; } } class ChildWidget extends Widget { - async mounted() { + mounted() { + expect(document.body.contains(this.el)).toBe(true); expect(parentMounted).toBe(true); childMounted = true; } @@ -153,6 +154,7 @@ describe("lifecycle hooks", () => { const target = document.createElement("div"); document.body.appendChild(target); await widget.mount(target); + expect(childMounted).toBe(true); target.remove(); }); });