[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
+33 -1
View File
@@ -1120,7 +1120,7 @@ describe("misc", () => {
});
});
describe.only("t-on", () => {
describe("t-on", () => {
test("can bind event handler", () => {
qweb.addTemplate("test", `<button t-on-click="add">Click</button>`);
let a = 1;
@@ -1324,6 +1324,38 @@ describe.only("t-on", () => {
expect(owner.state.n).toBe(4);
});
test("t-on with t-call", async () => {
expect.assertions(2);
qweb.addTemplate("sub", `<p t-on-click="update">lucas</p>`);
qweb.addTemplate("main", `<div><t t-call="sub"/></div>`);
let owner = {
update() {
expect(this).toBe(owner);
}
};
const node = renderToDOM(qweb, "main", owner, { handlers: [] });
(<HTMLElement>node).querySelector("p")!.click();
});
test("t-on, with arguments and t-call", async () => {
expect.assertions(3);
qweb.addTemplate("sub", `<p t-on-click="update(value)">lucas</p>`);
qweb.addTemplate("main", `<div><t t-call="sub"/></div>`);
let owner = {
update(val) {
expect(this).toBe(owner);
expect(val).toBe(444);
},
value: 444
};
const node = renderToDOM(qweb, "main", owner, { handlers: [] });
(<HTMLElement>node).querySelector("p")!.click();
});
test("t-on with prevent and/or stop modifiers", async () => {
expect.assertions(7);
qweb.addTemplate(