[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
+11 -1
View File
@@ -628,10 +628,20 @@ as it will be translated to:
```js
button.addEventListener("click", () => {
component.state.counter++;
context.state.counter++;
});
```
Warning: inline expressions are evaluated in the context of the template. This
means that they can access the component methods and properties. But if they set
a key, the inline statement will actually not modify the component, but a key in
a sub scope.
```xml
<button t-on-click="value = 1">Set value to 1 (does not work!!!)</button>
<button t-on-click="state.value = 1">Set state.value to 1 (work as expected)</button>
```
In order to remove the DOM event details from the event handlers (like calls to
`event.preventDefault`) and let them focus on data logic, _modifiers_ can be
specified as additional suffixes of the `t-on` directive.