mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
call mounted hooks at proper timing
This commit is contained in:
@@ -51,8 +51,6 @@ export class Widget<T extends WEnv> {
|
|||||||
async mount(target?: HTMLElement): Promise<VNode> {
|
async mount(target?: HTMLElement): Promise<VNode> {
|
||||||
await this.willStart();
|
await this.willStart();
|
||||||
this.isStarted = true;
|
this.isStarted = true;
|
||||||
this.env.qweb.addTemplate(this.name, this.template);
|
|
||||||
delete this.template;
|
|
||||||
const vnode = await this.render();
|
const vnode = await this.render();
|
||||||
|
|
||||||
if (target) {
|
if (target) {
|
||||||
@@ -96,6 +94,10 @@ export class Widget<T extends WEnv> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async _render(): Promise<VNode> {
|
private async _render(): Promise<VNode> {
|
||||||
|
if (this.template) {
|
||||||
|
this.env.qweb.addTemplate(this.name, this.template);
|
||||||
|
delete this.template;
|
||||||
|
}
|
||||||
const promises: Promise<void>[] = [];
|
const promises: Promise<void>[] = [];
|
||||||
let vnode = this.env.qweb.render(this.name, this, { promises });
|
let vnode = this.env.qweb.render(this.name, this, { promises });
|
||||||
return Promise.all(promises).then(() => vnode);
|
return Promise.all(promises).then(() => vnode);
|
||||||
|
|||||||
@@ -684,7 +684,7 @@ const widgetDirective: Directive = {
|
|||||||
`let _${widgetID} = new context.widgets['${value}'](context, ${props})`
|
`let _${widgetID} = new context.widgets['${value}'](context, ${props})`
|
||||||
);
|
);
|
||||||
ctx.addLine(
|
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})`);
|
ctx.addLine(`extra.promises.push(def${defID})`);
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ export class Counter extends Widget<Env> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mounted() {
|
mounted() {
|
||||||
debugger;
|
console.log("counter mounter", this.el);
|
||||||
}
|
}
|
||||||
increment(delta: number) {
|
increment(delta: number) {
|
||||||
this.updateState({ counter: this.state.counter + delta });
|
this.updateState({ counter: this.state.counter + delta });
|
||||||
|
|||||||
@@ -18,6 +18,9 @@ export class Navbar extends Widget<Env> {
|
|||||||
name = "navbar";
|
name = "navbar";
|
||||||
template = template;
|
template = template;
|
||||||
|
|
||||||
|
mounted() {
|
||||||
|
console.log("navbar mounted", this.el);
|
||||||
|
}
|
||||||
getUrl(menu: Menu) {
|
getUrl(menu: Menu) {
|
||||||
const action_id = String(menu.actionID);
|
const action_id = String(menu.actionID);
|
||||||
return this.env.router.formatURL("", { action_id });
|
return this.env.router.formatURL("", { action_id });
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ export class Clock extends Widget<Env> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mounted() {
|
mounted() {
|
||||||
|
console.log("clock mounter", this.el);
|
||||||
setInterval(this.updateTime.bind(this), 500);
|
setInterval(this.updateTime.bind(this), 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -131,20 +131,21 @@ describe("lifecycle hooks", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("mounted hook is called on subwidgets, in proper order", async () => {
|
test("mounted hook is called on subwidgets, in proper order", async () => {
|
||||||
expect.assertions(2);
|
expect.assertions(4);
|
||||||
let parentMounted = false;
|
let parentMounted = false;
|
||||||
let childMounted = false;
|
let childMounted = false;
|
||||||
class ParentWidget extends Widget<TestEnv> {
|
class ParentWidget extends Widget<TestEnv> {
|
||||||
name = "a";
|
name = "a";
|
||||||
template = `<div>Hello<t t-widget="child"/></div>`;
|
template = `<div>Hello<t t-widget="child"/></div>`;
|
||||||
widgets = { child: ChildWidget };
|
widgets = { child: ChildWidget };
|
||||||
async mounted() {
|
mounted() {
|
||||||
expect(childMounted).toBe(false);
|
expect(childMounted).toBe(false);
|
||||||
parentMounted = true;
|
parentMounted = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
class ChildWidget extends Widget<TestEnv> {
|
class ChildWidget extends Widget<TestEnv> {
|
||||||
async mounted() {
|
mounted() {
|
||||||
|
expect(document.body.contains(this.el)).toBe(true);
|
||||||
expect(parentMounted).toBe(true);
|
expect(parentMounted).toBe(true);
|
||||||
childMounted = true;
|
childMounted = true;
|
||||||
}
|
}
|
||||||
@@ -153,6 +154,7 @@ describe("lifecycle hooks", () => {
|
|||||||
const target = document.createElement("div");
|
const target = document.createElement("div");
|
||||||
document.body.appendChild(target);
|
document.body.appendChild(target);
|
||||||
await widget.mount(target);
|
await widget.mount(target);
|
||||||
|
expect(childMounted).toBe(true);
|
||||||
target.remove();
|
target.remove();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user