[FIX] qweb: bind handlers to component

Before this commit, the handlers were bound to the current context,
which is often the component but not necessarily.  In some cases, a sub
scope can be created (with t-foreach, or slots, or t-call), and the
context is actually an object with the actual component instance it its
prototype chain, but not the component.
This commit is contained in:
Géry Debongnie
2019-12-12 11:21:49 +01:00
committed by aab-odoo
parent 4e22dbcad6
commit 0931a4dc5b
11 changed files with 208 additions and 41 deletions
+38 -2
View File
@@ -2034,7 +2034,7 @@ describe("other directives with t-component", () => {
class Parent extends Component<any, any> {
static template = xml`<div><Child t-on-click="state.n = state.n + 1"/></div>`;
static components = { Child };
state = {n: 3};
state = { n: 3 };
}
const parent = new Parent();
await parent.mount(fixture);
@@ -2043,7 +2043,6 @@ describe("other directives with t-component", () => {
expect(parent.state.n).toBe(4);
});
test("t-on with handler bound to argument", async () => {
expect.assertions(3);
env.qweb.addTemplates(`
@@ -5559,3 +5558,40 @@ describe("t-raw in components", () => {
);
});
});
describe("t-call", () => {
test("handlers are properly bound through a t-call", async () => {
expect.assertions(3);
env.qweb.addTemplate("sub", `<p t-on-click="update">lucas</p>`);
class Parent extends Component<any, any> {
static template = xml`<div><t t-call="sub"/></div>`;
update() {
expect(this).toBe(parent);
}
}
const parent = new Parent();
await parent.mount(fixture);
expect(fixture.innerHTML).toBe("<div><p>lucas</p></div>");
fixture.querySelector("p")!.click();
expect(env.qweb.subTemplates["sub"].toString()).toMatchSnapshot();
});
test("handlers with arguments are properly bound through a t-call", async () => {
expect.assertions(3);
env.qweb.addTemplate("sub", `<p t-on-click="update(a)">lucas</p>`);
class Parent extends Component<any, any> {
static template = xml`<div><t t-call="sub"/></div>`;
update(a) {
expect(this).toBe(parent);
expect(a).toBe(3);
}
a = 3;
}
const parent = new Parent();
await parent.mount(fixture);
expect(env.qweb.subTemplates["sub"].toString()).toMatchSnapshot();
fixture.querySelector("p")!.click();
});
});