diff --git a/doc/component.md b/doc/component.md
index 3b548f78..e940d63e 100644
--- a/doc/component.md
+++ b/doc/component.md
@@ -677,6 +677,29 @@ to event `menu-loaded` will receive the payload in its `someMethod` handler
By convention, we use KebabCase for the name of _business_ events.
+
+The `t-on` directive allows to prebind some arguments. For example,
+
+```xml
+
+```
+
+Here, `expr` is a valid Owl expression, so it could be `true` or some variable
+from the rendering context.
+
+One can also directly specify inline statements. For example,
+
+```xml
+
+```
+
+Here, `state` must be defined in the rendering context (typically the component)
+as it will be translated to:
+```js
+button.addEventListener("click", () => { component.state.counter++; });
+```
+
+
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.
@@ -696,15 +719,6 @@ the order may matter. For instance `t-on-click.prevent.self` will prevent all
clicks while `t-on-click.self.prevent` will only prevent clicks on the element
itself.
-The `t-on` directive also allows to prebind some arguments. For example,
-
-```xml
-
-```
-
-Here, `expr` is a valid Owl expression, so it could be `true` or some variable
-from the rendering context.
-
### Form Input Bindings
It is very common to need to be able to read the value out of an html `input` (or
diff --git a/src/component/directive.ts b/src/component/directive.ts
index f1ebed37..7a20ba84 100644
--- a/src/component/directive.ts
+++ b/src/component/directive.ts
@@ -209,11 +209,11 @@ QWeb.addDirective({
if (name.startsWith("t-on-")) {
const [eventName, ...mods] = name.slice(5).split(".");
let extraArgs;
- let handlerName = value.replace(/\(.*\)/, function(args) {
+ let handlerValue = value.replace(/\(.*\)/, function(args) {
extraArgs = args.slice(1, -1);
return "";
});
- events.push([eventName, mods, handlerName, extraArgs]);
+ events.push([eventName, mods, handlerValue, extraArgs]);
} else if (name === "t-transition") {
transition = value;
} else if (!name.startsWith("t-")) {
@@ -315,7 +315,7 @@ QWeb.addDirective({
}
}
let eventsCode = events
- .map(function([eventName, mods, handlerName, extraArgs]) {
+ .map(function([eventName, mods, handlerValue, extraArgs]) {
let params = "owner";
if (extraArgs) {
if (ctx.loopNumber) {
@@ -329,18 +329,15 @@ QWeb.addDirective({
params = `owner, ${ctx.formatExpression(extraArgs)}`;
}
}
- let handler;
- if (mods.length > 0) {
- handler = `function (e) {`;
- handler += mods
- .map(function(mod) {
- return T_COMPONENT_MODS_CODE[mod];
- })
- .join("");
- handler += `owner['${handlerName}'].call(${params}, e);}`;
- } else {
- handler = `owner['${handlerName}'].bind(${params})`;
- }
+ let handler = `function (e) {`;
+ handler += mods
+ .map(function(mod) {
+ return T_COMPONENT_MODS_CODE[mod];
+ })
+ .join("");
+ handler += `const fn = owner['${handlerValue}'];`;
+ handler += `if (fn) { fn.call(${params}, e); } else { owner.${handlerValue}; }`;
+ handler += `}`;
return `vn.elm.addEventListener('${eventName}', ${handler});`;
})
.join("");
diff --git a/src/qweb/extensions.ts b/src/qweb/extensions.ts
index 7e9cdb96..aefc20f2 100644
--- a/src/qweb/extensions.ts
+++ b/src/qweb/extensions.ts
@@ -40,44 +40,27 @@ QWeb.addDirective({
extraArgs = args.slice(1, -1);
return "";
});
- ctx.addIf(`!context['${handlerName}']`);
- ctx.addLine(
- `throw new Error('Missing handler \\'' + '${handlerName}' + \`\\' when evaluating template '${ctx.templateName.replace(
- /`/g,
- "'"
- )}'\`)`
- );
- ctx.closeIf();
let params = extraArgs ? `owner, ${ctx.formatExpression(extraArgs)}` : "owner";
- if (mods.length > 0) {
- let handler = `function (e) {`;
- handler += mods
- .map(function(mod) {
- return MODS_CODE[mod];
- })
- .join("");
- if (!extraArgs) {
- handler += `context['${handlerName}'].call(${params}, e);}`;
- ctx.addLine(
- `extra.handlers['${eventName}' + ${nodeID}] = extra.handlers['${eventName}' + ${nodeID}] || ${handler};`
- );
- ctx.addLine(`p${nodeID}.on['${eventName}'] = extra.handlers['${eventName}' + ${nodeID}];`);
- } else {
- const handlerKey = `handler${ctx.generateID()}`;
- ctx.addLine(`const ${handlerKey} = context['${handlerName}'].bind(${params});`);
- handler += `${handlerKey}(e);}`;
- ctx.addLine(`p${nodeID}.on['${eventName}'] = ${handler};`);
- }
+ let handler = `function (e) {`;
+ handler += mods
+ .map(function(mod) {
+ return MODS_CODE[mod];
+ })
+ .join("");
+ if (!extraArgs) {
+ handler += `const fn = context['${handlerName}'];`;
+ handler += `if (fn) { fn.call(${params}, e); } else { context.${handlerName}; }`;
+ handler += `}`;
+ ctx.addLine(
+ `extra.handlers['${eventName}' + ${nodeID}] = extra.handlers['${eventName}' + ${nodeID}] || ${handler};`
+ );
+ ctx.addLine(`p${nodeID}.on['${eventName}'] = extra.handlers['${eventName}' + ${nodeID}];`);
} else {
- const handler = `context['${handlerName}'].bind(${params})`;
- if (extraArgs) {
- ctx.addLine(`p${nodeID}.on['${eventName}'] = ${handler};`);
- } else {
- ctx.addLine(
- `extra.handlers['${eventName}' + ${nodeID}] = extra.handlers['${eventName}' + ${nodeID}] || ${handler};`
- );
- ctx.addLine(`p${nodeID}.on['${eventName}'] = extra.handlers['${eventName}' + ${nodeID}];`);
- }
+ const handlerKey = `handler${ctx.generateID()}`;
+ ctx.addLine(`const ${handlerKey} = context['${handlerName}'] && context['${handlerName}'].bind(${params});`);
+ handler += `if (${handlerKey}) { ${handlerKey}(e); } else { context.${value}; }`;
+ handler += `}`;
+ ctx.addLine(`p${nodeID}.on['${eventName}'] = ${handler};`);
}
}
});
diff --git a/tests/component/__snapshots__/component.test.ts.snap b/tests/component/__snapshots__/component.test.ts.snap
index 990ee0cd..58d11b50 100644
--- a/tests/component/__snapshots__/component.test.ts.snap
+++ b/tests/component/__snapshots__/component.test.ts.snap
@@ -13,10 +13,7 @@ exports[`async rendering delayed component with t-asyncroot directive 1`] = `
let c2 = [], p2 = {key:2,on:{}};
var vn2 = h('button', p2, c2);
c1.push(vn2);
- if (!context['updateApp']) {
- throw new Error('Missing handler \\\\'' + 'updateApp' + \`\\\\' when evaluating template 'Parent'\`)
- }
- extra.handlers['click' + 2] = extra.handlers['click' + 2] || context['updateApp'].bind(owner);
+ extra.handlers['click' + 2] = extra.handlers['click' + 2] || function (e) {const fn = context['updateApp'];if (fn) { fn.call(owner, e); } else { context.updateApp; }};
p2.on['click'] = extra.handlers['click' + 2];
c2.push({text: \`Update App State\`});
let _4 = {'children':true};
@@ -97,10 +94,7 @@ exports[`async rendering fast component with t-asyncroot directive 1`] = `
let c2 = [], p2 = {key:2,on:{}};
var vn2 = h('button', p2, c2);
c1.push(vn2);
- if (!context['updateApp']) {
- throw new Error('Missing handler \\\\'' + 'updateApp' + \`\\\\' when evaluating template 'Parent'\`)
- }
- extra.handlers['click' + 2] = extra.handlers['click' + 2] || context['updateApp'].bind(owner);
+ extra.handlers['click' + 2] = extra.handlers['click' + 2] || function (e) {const fn = context['updateApp'];if (fn) { fn.call(owner, e); } else { context.updateApp; }};
p2.on['click'] = extra.handlers['click' + 2];
c2.push({text: \`Update App State\`});
let _4 = {'children':true};
@@ -181,10 +175,7 @@ exports[`async rendering t-component with t-asyncroot directive: mixed re-render
let c2 = [], p2 = {key:2,on:{}};
var vn2 = h('button', p2, c2);
c1.push(vn2);
- if (!context['updateApp']) {
- throw new Error('Missing handler \\\\'' + 'updateApp' + \`\\\\' when evaluating template 'Parent'\`)
- }
- extra.handlers['click' + 2] = extra.handlers['click' + 2] || context['updateApp'].bind(owner);
+ extra.handlers['click' + 2] = extra.handlers['click' + 2] || function (e) {const fn = context['updateApp'];if (fn) { fn.call(owner, e); } else { context.updateApp; }};
p2.on['click'] = extra.handlers['click' + 2];
c2.push({text: \`Update App State\`});
let _4 = {'children':true};
@@ -807,6 +798,52 @@ exports[`lifecycle hooks willPatch/patched hook with t-keepalive 1`] = `
}"
`;
+exports[`other directives with t-component t-on with getter as handler 1`] = `
+"function anonymous(context,extra
+) {
+ let utils = this.constructor.utils;
+ let QWeb = this.constructor;
+ let parent = context;
+ let owner = context;
+ var h = this.h;
+ let c1 = [], p1 = {key:1};
+ var vn1 = h('div', p1, c1);
+ var _2 = context['state'].counter;
+ if (_2 || _2 === 0) {
+ c1.push({text: _2});
+ }
+ //COMPONENT
+ let def4;
+ let templateId5 = \`__6__\`;
+ let w5 = templateId5 in parent.__owl__.cmap ? parent.__owl__.children[parent.__owl__.cmap[templateId5]] : false;
+ let _3_index = c1.length;
+ c1.push(null);
+ let props5 = {};
+ if (w5 && w5.__owl__.currentFiber && !w5.__owl__.vnode) {
+ if (utils.shallowEqual(props5, w5.__owl__.currentFiber.props)) {
+ def4 = w5.__owl__.currentFiber.promise;
+ } else {
+ w5.destroy();
+ w5 = false;
+ }
+ }
+ if (!w5) {
+ let componentKey5 = \`Child\`;
+ let W5 = context.constructor.components[componentKey5] || QWeb.components[componentKey5]|| context['Child'];
+ if (!W5) {throw new Error('Cannot find the definition of component \\"' + componentKey5 + '\\"')}
+ w5 = new W5(parent, props5);
+ parent.__owl__.cmap[templateId5] = w5.__owl__.id;
+ def4 = w5.__prepare(extra.fiber, undefined, undefined);
+ def4 = def4.then(vnode=>{if (w5.__owl__.isDestroyed){return}vnode.data.hook = {create(_, vn){vn.elm.addEventListener('ev', function (e) {const fn = owner['handler'];if (fn) { fn.call(owner, e); } else { owner.handler; }});}};let pvnode=h(vnode.sel, {key: templateId5, hook: {insert(vn) {let nvn=w5.__mount(vnode, pvnode.elm);pvnode.elm=nvn.elm;},remove() {},destroy(vn) {w5.destroy();}}});c1[_3_index]=pvnode;w5.__owl__.pvnode = pvnode;});
+ } else {
+ def4 = def4 || w5.__updateProps(props5, extra.fiber, undefined, undefined);
+ def4 = def4.then(()=>{if (w5.__owl__.isDestroyed) {return};let pvnode=w5.__owl__.pvnode;c1[_3_index]=pvnode;});
+ }
+ extra.promises.push(def4);
+ return vn1;
+}"
+`;
+
exports[`other directives with t-component t-on with handler bound to argument 1`] = `
"function anonymous(context,extra
) {
@@ -839,7 +876,7 @@ exports[`other directives with t-component t-on with handler bound to argument 1
w4 = new W4(parent, props4);
parent.__owl__.cmap[templateId4] = w4.__owl__.id;
def3 = w4.__prepare(extra.fiber, undefined, undefined);
- def3 = def3.then(vnode=>{if (w4.__owl__.isDestroyed){return}vnode.data.hook = {create(_, vn){vn.elm.addEventListener('ev', owner['onEv'].bind(owner, 3));}};let pvnode=h(vnode.sel, {key: templateId4, hook: {insert(vn) {let nvn=w4.__mount(vnode, pvnode.elm);pvnode.elm=nvn.elm;},remove() {},destroy(vn) {w4.destroy();}}});c1[_2_index]=pvnode;w4.__owl__.pvnode = pvnode;});
+ def3 = def3.then(vnode=>{if (w4.__owl__.isDestroyed){return}vnode.data.hook = {create(_, vn){vn.elm.addEventListener('ev', function (e) {const fn = owner['onEv'];if (fn) { fn.call(owner, 3, e); } else { owner.onEv; }});}};let pvnode=h(vnode.sel, {key: templateId4, hook: {insert(vn) {let nvn=w4.__mount(vnode, pvnode.elm);pvnode.elm=nvn.elm;},remove() {},destroy(vn) {w4.destroy();}}});c1[_2_index]=pvnode;w4.__owl__.pvnode = pvnode;});
} else {
def3 = def3 || w4.__updateProps(props4, extra.fiber, undefined, undefined);
def3 = def3.then(()=>{if (w4.__owl__.isDestroyed) {return};let pvnode=w4.__owl__.pvnode;c1[_2_index]=pvnode;});
@@ -881,7 +918,7 @@ exports[`other directives with t-component t-on with handler bound to empty obje
w4 = new W4(parent, props4);
parent.__owl__.cmap[templateId4] = w4.__owl__.id;
def3 = w4.__prepare(extra.fiber, undefined, undefined);
- def3 = def3.then(vnode=>{if (w4.__owl__.isDestroyed){return}vnode.data.hook = {create(_, vn){vn.elm.addEventListener('ev', owner['onEv'].bind(owner, {}));}};let pvnode=h(vnode.sel, {key: templateId4, hook: {insert(vn) {let nvn=w4.__mount(vnode, pvnode.elm);pvnode.elm=nvn.elm;},remove() {},destroy(vn) {w4.destroy();}}});c1[_2_index]=pvnode;w4.__owl__.pvnode = pvnode;});
+ def3 = def3.then(vnode=>{if (w4.__owl__.isDestroyed){return}vnode.data.hook = {create(_, vn){vn.elm.addEventListener('ev', function (e) {const fn = owner['onEv'];if (fn) { fn.call(owner, {}, e); } else { owner.onEv; }});}};let pvnode=h(vnode.sel, {key: templateId4, hook: {insert(vn) {let nvn=w4.__mount(vnode, pvnode.elm);pvnode.elm=nvn.elm;},remove() {},destroy(vn) {w4.destroy();}}});c1[_2_index]=pvnode;w4.__owl__.pvnode = pvnode;});
} else {
def3 = def3 || w4.__updateProps(props4, extra.fiber, undefined, undefined);
def3 = def3.then(()=>{if (w4.__owl__.isDestroyed) {return};let pvnode=w4.__owl__.pvnode;c1[_2_index]=pvnode;});
@@ -923,7 +960,7 @@ exports[`other directives with t-component t-on with handler bound to empty obje
w4 = new W4(parent, props4);
parent.__owl__.cmap[templateId4] = w4.__owl__.id;
def3 = w4.__prepare(extra.fiber, undefined, undefined);
- def3 = def3.then(vnode=>{if (w4.__owl__.isDestroyed){return}vnode.data.hook = {create(_, vn){vn.elm.addEventListener('ev', owner['onEv'].bind(owner, {}));}};let pvnode=h(vnode.sel, {key: templateId4, hook: {insert(vn) {let nvn=w4.__mount(vnode, pvnode.elm);pvnode.elm=nvn.elm;},remove() {},destroy(vn) {w4.destroy();}}});c1[_2_index]=pvnode;w4.__owl__.pvnode = pvnode;});
+ def3 = def3.then(vnode=>{if (w4.__owl__.isDestroyed){return}vnode.data.hook = {create(_, vn){vn.elm.addEventListener('ev', function (e) {const fn = owner['onEv'];if (fn) { fn.call(owner, {}, e); } else { owner.onEv; }});}};let pvnode=h(vnode.sel, {key: templateId4, hook: {insert(vn) {let nvn=w4.__mount(vnode, pvnode.elm);pvnode.elm=nvn.elm;},remove() {},destroy(vn) {w4.destroy();}}});c1[_2_index]=pvnode;w4.__owl__.pvnode = pvnode;});
} else {
def3 = def3 || w4.__updateProps(props4, extra.fiber, undefined, undefined);
def3 = def3.then(()=>{if (w4.__owl__.isDestroyed) {return};let pvnode=w4.__owl__.pvnode;c1[_2_index]=pvnode;});
@@ -965,7 +1002,7 @@ exports[`other directives with t-component t-on with handler bound to object 1`]
w4 = new W4(parent, props4);
parent.__owl__.cmap[templateId4] = w4.__owl__.id;
def3 = w4.__prepare(extra.fiber, undefined, undefined);
- def3 = def3.then(vnode=>{if (w4.__owl__.isDestroyed){return}vnode.data.hook = {create(_, vn){vn.elm.addEventListener('ev', owner['onEv'].bind(owner, {val:3}));}};let pvnode=h(vnode.sel, {key: templateId4, hook: {insert(vn) {let nvn=w4.__mount(vnode, pvnode.elm);pvnode.elm=nvn.elm;},remove() {},destroy(vn) {w4.destroy();}}});c1[_2_index]=pvnode;w4.__owl__.pvnode = pvnode;});
+ def3 = def3.then(vnode=>{if (w4.__owl__.isDestroyed){return}vnode.data.hook = {create(_, vn){vn.elm.addEventListener('ev', function (e) {const fn = owner['onEv'];if (fn) { fn.call(owner, {val:3}, e); } else { owner.onEv; }});}};let pvnode=h(vnode.sel, {key: templateId4, hook: {insert(vn) {let nvn=w4.__mount(vnode, pvnode.elm);pvnode.elm=nvn.elm;},remove() {},destroy(vn) {w4.destroy();}}});c1[_2_index]=pvnode;w4.__owl__.pvnode = pvnode;});
} else {
def3 = def3 || w4.__updateProps(props4, extra.fiber, undefined, undefined);
def3 = def3.then(()=>{if (w4.__owl__.isDestroyed) {return};let pvnode=w4.__owl__.pvnode;c1[_2_index]=pvnode;});
@@ -975,6 +1012,52 @@ exports[`other directives with t-component t-on with handler bound to object 1`]
}"
`;
+exports[`other directives with t-component t-on with inline statement 1`] = `
+"function anonymous(context,extra
+) {
+ let utils = this.constructor.utils;
+ let QWeb = this.constructor;
+ let parent = context;
+ let owner = context;
+ var h = this.h;
+ let c1 = [], p1 = {key:1};
+ var vn1 = h('div', p1, c1);
+ var _2 = context['state'].counter;
+ if (_2 || _2 === 0) {
+ c1.push({text: _2});
+ }
+ //COMPONENT
+ let def4;
+ let templateId5 = \`__6__\`;
+ let w5 = templateId5 in parent.__owl__.cmap ? parent.__owl__.children[parent.__owl__.cmap[templateId5]] : false;
+ let _3_index = c1.length;
+ c1.push(null);
+ let props5 = {};
+ if (w5 && w5.__owl__.currentFiber && !w5.__owl__.vnode) {
+ if (utils.shallowEqual(props5, w5.__owl__.currentFiber.props)) {
+ def4 = w5.__owl__.currentFiber.promise;
+ } else {
+ w5.destroy();
+ w5 = false;
+ }
+ }
+ if (!w5) {
+ let componentKey5 = \`Child\`;
+ let W5 = context.constructor.components[componentKey5] || QWeb.components[componentKey5]|| context['Child'];
+ if (!W5) {throw new Error('Cannot find the definition of component \\"' + componentKey5 + '\\"')}
+ w5 = new W5(parent, props5);
+ parent.__owl__.cmap[templateId5] = w5.__owl__.id;
+ def4 = w5.__prepare(extra.fiber, undefined, undefined);
+ def4 = def4.then(vnode=>{if (w5.__owl__.isDestroyed){return}vnode.data.hook = {create(_, vn){vn.elm.addEventListener('ev', function (e) {const fn = owner['state.counter++'];if (fn) { fn.call(owner, e); } else { owner.state.counter++; }});}};let pvnode=h(vnode.sel, {key: templateId5, hook: {insert(vn) {let nvn=w5.__mount(vnode, pvnode.elm);pvnode.elm=nvn.elm;},remove() {},destroy(vn) {w5.destroy();}}});c1[_3_index]=pvnode;w5.__owl__.pvnode = pvnode;});
+ } else {
+ def4 = def4 || w5.__updateProps(props5, extra.fiber, undefined, undefined);
+ def4 = def4.then(()=>{if (w5.__owl__.isDestroyed) {return};let pvnode=w5.__owl__.pvnode;c1[_3_index]=pvnode;});
+ }
+ extra.promises.push(def4);
+ return vn1;
+}"
+`;
+
exports[`other directives with t-component t-on with prevent and self modifiers (order matters) 1`] = `
"function anonymous(context,extra
) {
@@ -1007,7 +1090,7 @@ exports[`other directives with t-component t-on with prevent and self modifiers
w4 = new W4(parent, props4);
parent.__owl__.cmap[templateId4] = w4.__owl__.id;
def3 = w4.__prepare(extra.fiber, undefined, undefined);
- def3 = def3.then(vnode=>{if (w4.__owl__.isDestroyed){return}vnode.data.hook = {create(_, vn){vn.elm.addEventListener('ev', function (e) {e.preventDefault();if (e.target !== vn.elm) {return}owner['onEv'].call(owner, e);});}};let pvnode=h(vnode.sel, {key: templateId4, hook: {insert(vn) {let nvn=w4.__mount(vnode, pvnode.elm);pvnode.elm=nvn.elm;},remove() {},destroy(vn) {w4.destroy();}}});c1[_2_index]=pvnode;w4.__owl__.pvnode = pvnode;});
+ def3 = def3.then(vnode=>{if (w4.__owl__.isDestroyed){return}vnode.data.hook = {create(_, vn){vn.elm.addEventListener('ev', function (e) {e.preventDefault();if (e.target !== vn.elm) {return}const fn = owner['onEv'];if (fn) { fn.call(owner, e); } else { owner.onEv; }});}};let pvnode=h(vnode.sel, {key: templateId4, hook: {insert(vn) {let nvn=w4.__mount(vnode, pvnode.elm);pvnode.elm=nvn.elm;},remove() {},destroy(vn) {w4.destroy();}}});c1[_2_index]=pvnode;w4.__owl__.pvnode = pvnode;});
} else {
def3 = def3 || w4.__updateProps(props4, extra.fiber, undefined, undefined);
def3 = def3.then(()=>{if (w4.__owl__.isDestroyed) {return};let pvnode=w4.__owl__.pvnode;c1[_2_index]=pvnode;});
@@ -1049,7 +1132,7 @@ exports[`other directives with t-component t-on with self and prevent modifiers
w4 = new W4(parent, props4);
parent.__owl__.cmap[templateId4] = w4.__owl__.id;
def3 = w4.__prepare(extra.fiber, undefined, undefined);
- def3 = def3.then(vnode=>{if (w4.__owl__.isDestroyed){return}vnode.data.hook = {create(_, vn){vn.elm.addEventListener('ev', function (e) {if (e.target !== vn.elm) {return}e.preventDefault();owner['onEv'].call(owner, e);});}};let pvnode=h(vnode.sel, {key: templateId4, hook: {insert(vn) {let nvn=w4.__mount(vnode, pvnode.elm);pvnode.elm=nvn.elm;},remove() {},destroy(vn) {w4.destroy();}}});c1[_2_index]=pvnode;w4.__owl__.pvnode = pvnode;});
+ def3 = def3.then(vnode=>{if (w4.__owl__.isDestroyed){return}vnode.data.hook = {create(_, vn){vn.elm.addEventListener('ev', function (e) {if (e.target !== vn.elm) {return}e.preventDefault();const fn = owner['onEv'];if (fn) { fn.call(owner, e); } else { owner.onEv; }});}};let pvnode=h(vnode.sel, {key: templateId4, hook: {insert(vn) {let nvn=w4.__mount(vnode, pvnode.elm);pvnode.elm=nvn.elm;},remove() {},destroy(vn) {w4.destroy();}}});c1[_2_index]=pvnode;w4.__owl__.pvnode = pvnode;});
} else {
def3 = def3 || w4.__updateProps(props4, extra.fiber, undefined, undefined);
def3 = def3.then(()=>{if (w4.__owl__.isDestroyed) {return};let pvnode=w4.__owl__.pvnode;c1[_2_index]=pvnode;});
@@ -1091,7 +1174,7 @@ exports[`other directives with t-component t-on with self modifier 1`] = `
w4 = new W4(parent, props4);
parent.__owl__.cmap[templateId4] = w4.__owl__.id;
def3 = w4.__prepare(extra.fiber, undefined, undefined);
- def3 = def3.then(vnode=>{if (w4.__owl__.isDestroyed){return}vnode.data.hook = {create(_, vn){vn.elm.addEventListener('ev-1', owner['onEv1'].bind(owner));vn.elm.addEventListener('ev-2', function (e) {if (e.target !== vn.elm) {return}owner['onEv2'].call(owner, e);});}};let pvnode=h(vnode.sel, {key: templateId4, hook: {insert(vn) {let nvn=w4.__mount(vnode, pvnode.elm);pvnode.elm=nvn.elm;},remove() {},destroy(vn) {w4.destroy();}}});c1[_2_index]=pvnode;w4.__owl__.pvnode = pvnode;});
+ def3 = def3.then(vnode=>{if (w4.__owl__.isDestroyed){return}vnode.data.hook = {create(_, vn){vn.elm.addEventListener('ev-1', function (e) {const fn = owner['onEv1'];if (fn) { fn.call(owner, e); } else { owner.onEv1; }});vn.elm.addEventListener('ev-2', function (e) {if (e.target !== vn.elm) {return}const fn = owner['onEv2'];if (fn) { fn.call(owner, e); } else { owner.onEv2; }});}};let pvnode=h(vnode.sel, {key: templateId4, hook: {insert(vn) {let nvn=w4.__mount(vnode, pvnode.elm);pvnode.elm=nvn.elm;},remove() {},destroy(vn) {w4.destroy();}}});c1[_2_index]=pvnode;w4.__owl__.pvnode = pvnode;});
} else {
def3 = def3 || w4.__updateProps(props4, extra.fiber, undefined, undefined);
def3 = def3.then(()=>{if (w4.__owl__.isDestroyed) {return};let pvnode=w4.__owl__.pvnode;c1[_2_index]=pvnode;});
@@ -1133,7 +1216,7 @@ exports[`other directives with t-component t-on with stop and/or prevent modifie
w4 = new W4(parent, props4);
parent.__owl__.cmap[templateId4] = w4.__owl__.id;
def3 = w4.__prepare(extra.fiber, undefined, undefined);
- def3 = def3.then(vnode=>{if (w4.__owl__.isDestroyed){return}vnode.data.hook = {create(_, vn){vn.elm.addEventListener('ev-1', function (e) {e.stopPropagation();owner['onEv1'].call(owner, e);});vn.elm.addEventListener('ev-2', function (e) {e.preventDefault();owner['onEv2'].call(owner, e);});vn.elm.addEventListener('ev-3', function (e) {e.stopPropagation();e.preventDefault();owner['onEv3'].call(owner, e);});}};let pvnode=h(vnode.sel, {key: templateId4, hook: {insert(vn) {let nvn=w4.__mount(vnode, pvnode.elm);pvnode.elm=nvn.elm;},remove() {},destroy(vn) {w4.destroy();}}});c1[_2_index]=pvnode;w4.__owl__.pvnode = pvnode;});
+ def3 = def3.then(vnode=>{if (w4.__owl__.isDestroyed){return}vnode.data.hook = {create(_, vn){vn.elm.addEventListener('ev-1', function (e) {e.stopPropagation();const fn = owner['onEv1'];if (fn) { fn.call(owner, e); } else { owner.onEv1; }});vn.elm.addEventListener('ev-2', function (e) {e.preventDefault();const fn = owner['onEv2'];if (fn) { fn.call(owner, e); } else { owner.onEv2; }});vn.elm.addEventListener('ev-3', function (e) {e.stopPropagation();e.preventDefault();const fn = owner['onEv3'];if (fn) { fn.call(owner, e); } else { owner.onEv3; }});}};let pvnode=h(vnode.sel, {key: templateId4, hook: {insert(vn) {let nvn=w4.__mount(vnode, pvnode.elm);pvnode.elm=nvn.elm;},remove() {},destroy(vn) {w4.destroy();}}});c1[_2_index]=pvnode;w4.__owl__.pvnode = pvnode;});
} else {
def3 = def3 || w4.__updateProps(props4, extra.fiber, undefined, undefined);
def3 = def3.then(()=>{if (w4.__owl__.isDestroyed) {return};let pvnode=w4.__owl__.pvnode;c1[_2_index]=pvnode;});
@@ -1252,7 +1335,7 @@ exports[`random stuff/miscellaneous t-on with handler bound to dynamic argument
w7 = new W7(parent, props7);
parent.__owl__.cmap[templateId7] = w7.__owl__.id;
def6 = w7.__prepare(extra.fiber, undefined, undefined);
- def6 = def6.then(vnode=>{if (w7.__owl__.isDestroyed){return}vnode.data.hook = {create(_, vn){vn.elm.addEventListener('ev', owner['onEv'].bind(owner, arg10));}};let pvnode=h(vnode.sel, {key: templateId7, hook: {insert(vn) {let nvn=w7.__mount(vnode, pvnode.elm);pvnode.elm=nvn.elm;},remove() {},destroy(vn) {w7.destroy();}}});c1[_5_index]=pvnode;w7.__owl__.pvnode = pvnode;});
+ def6 = def6.then(vnode=>{if (w7.__owl__.isDestroyed){return}vnode.data.hook = {create(_, vn){vn.elm.addEventListener('ev', function (e) {const fn = owner['onEv'];if (fn) { fn.call(owner, arg10, e); } else { owner.onEv; }});}};let pvnode=h(vnode.sel, {key: templateId7, hook: {insert(vn) {let nvn=w7.__mount(vnode, pvnode.elm);pvnode.elm=nvn.elm;},remove() {},destroy(vn) {w7.destroy();}}});c1[_5_index]=pvnode;w7.__owl__.pvnode = pvnode;});
} else {
def6 = def6 || w7.__updateProps(props7, extra.fiber, undefined, undefined);
def6 = def6.then(()=>{if (w7.__owl__.isDestroyed) {return};let pvnode=w7.__owl__.pvnode;c1[_5_index]=pvnode;});
@@ -1623,10 +1706,7 @@ exports[`t-slot directive refs are properly bound in slots 1`] = `
let c11 = [], p11 = {key:11,on:{}};
var vn11 = h('button', p11, c11);
c1.push(vn11);
- if (!context['doSomething']) {
- throw new Error('Missing handler \\\\'' + 'doSomething' + \`\\\\' when evaluating template 'slot_footer_template'\`)
- }
- extra.handlers['click' + 11] = extra.handlers['click' + 11] || context['doSomething'].bind(owner);
+ extra.handlers['click' + 11] = extra.handlers['click' + 11] || function (e) {const fn = context['doSomething'];if (fn) { fn.call(owner, e); } else { context.doSomething; }};
p11.on['click'] = extra.handlers['click' + 11];
const ref12 = \`myButton\`;
p11.hook = {
@@ -1651,10 +1731,7 @@ exports[`t-slot directive slots are rendered with proper context 1`] = `
let c11 = [], p11 = {key:11,on:{}};
var vn11 = h('button', p11, c11);
c1.push(vn11);
- if (!context['doSomething']) {
- throw new Error('Missing handler \\\\'' + 'doSomething' + \`\\\\' when evaluating template 'slot_footer_template'\`)
- }
- extra.handlers['click' + 11] = extra.handlers['click' + 11] || context['doSomething'].bind(owner);
+ extra.handlers['click' + 11] = extra.handlers['click' + 11] || function (e) {const fn = context['doSomething'];if (fn) { fn.call(owner, e); } else { context.doSomething; }};
p11.on['click'] = extra.handlers['click' + 11];
c11.push({text: \`do something\`});
}"
diff --git a/tests/component/component.test.ts b/tests/component/component.test.ts
index a813e220..027a7ca2 100644
--- a/tests/component/component.test.ts
+++ b/tests/component/component.test.ts
@@ -2010,6 +2010,60 @@ describe("other directives with t-component", () => {
expect(env.qweb.templates.ParentWidget.fn.toString()).toMatchSnapshot();
});
+ test("t-on with getter as handler", async () => {
+ class Child extends Component {
+ static template = xml``;
+ }
+ class Parent extends Component {
+ static template = xml`
+
+
+
+
`;
+ static components = { Child };
+ state = useState({counter: 0});
+ get handler() {
+ this.state.counter++;
+ return () => {};
+ }
+ }
+ const parent = new Parent(env);
+ await parent.mount(fixture);
+
+ expect(env.qweb.templates[Parent.template].fn.toString()).toMatchSnapshot();
+ expect(fixture.innerHTML).toBe('0
');
+
+ let child = children(parent)[0];
+ child.trigger("ev");
+ await nextTick();
+ expect(fixture.innerHTML).toBe('1
');
+ });
+
+ test("t-on with inline statement", async () => {
+ class Child extends Component {
+ static template = xml``;
+ }
+ class Parent extends Component {
+ static template = xml`
+
+
+
+
`;
+ static components = { Child };
+ state = useState({counter: 0});
+ }
+ const parent = new Parent(env);
+ await parent.mount(fixture);
+
+ expect(env.qweb.templates[Parent.template].fn.toString()).toMatchSnapshot();
+ expect(fixture.innerHTML).toBe('0
');
+
+ let child = children(parent)[0];
+ child.trigger("ev");
+ await nextTick();
+ expect(fixture.innerHTML).toBe('1
');
+ });
+
test("t-if works with t-component", async () => {
env.qweb.addTemplate("ParentWidget", `
`);
class Child extends Widget {}
diff --git a/tests/qweb/__snapshots__/qweb.test.ts.snap b/tests/qweb/__snapshots__/qweb.test.ts.snap
index e0cf231d..186f1806 100644
--- a/tests/qweb/__snapshots__/qweb.test.ts.snap
+++ b/tests/qweb/__snapshots__/qweb.test.ts.snap
@@ -1618,10 +1618,7 @@ exports[`t-on can bind event handler 1`] = `
var h = this.h;
let c1 = [], p1 = {key:1,on:{}};
var vn1 = h('button', p1, c1);
- if (!context['add']) {
- throw new Error('Missing handler \\\\'' + 'add' + \`\\\\' when evaluating template 'test'\`)
- }
- extra.handlers['click' + 1] = extra.handlers['click' + 1] || context['add'].bind(owner);
+ extra.handlers['click' + 1] = extra.handlers['click' + 1] || function (e) {const fn = context['add'];if (fn) { fn.call(owner, e); } else { context.add; }};
p1.on['click'] = extra.handlers['click' + 1];
c1.push({text: \`Click\`});
return vn1;
@@ -1635,10 +1632,8 @@ exports[`t-on can bind handlers with arguments 1`] = `
var h = this.h;
let c1 = [], p1 = {key:1,on:{}};
var vn1 = h('button', p1, c1);
- if (!context['add']) {
- throw new Error('Missing handler \\\\'' + 'add' + \`\\\\' when evaluating template 'test'\`)
- }
- p1.on['click'] = context['add'].bind(owner, 5);
+ const handler2 = context['add'] && context['add'].bind(owner, 5);
+ p1.on['click'] = function (e) {if (handler2) { handler2(e); } else { context.add(5); }};
c1.push({text: \`Click\`});
return vn1;
}"
@@ -1651,10 +1646,8 @@ exports[`t-on can bind handlers with empty object (with non empty inner string)
var h = this.h;
let c1 = [], p1 = {key:1,on:{}};
var vn1 = h('button', p1, c1);
- if (!context['doSomething']) {
- throw new Error('Missing handler \\\\'' + 'doSomething' + \`\\\\' when evaluating template 'test'\`)
- }
- p1.on['click'] = context['doSomething'].bind(owner, {});
+ const handler2 = context['doSomething'] && context['doSomething'].bind(owner, {});
+ p1.on['click'] = function (e) {if (handler2) { handler2(e); } else { context.doSomething({ }); }};
c1.push({text: \`Click\`});
return vn1;
}"
@@ -1667,10 +1660,8 @@ exports[`t-on can bind handlers with empty object 1`] = `
var h = this.h;
let c1 = [], p1 = {key:1,on:{}};
var vn1 = h('button', p1, c1);
- if (!context['doSomething']) {
- throw new Error('Missing handler \\\\'' + 'doSomething' + \`\\\\' when evaluating template 'test'\`)
- }
- p1.on['click'] = context['doSomething'].bind(owner, {});
+ const handler2 = context['doSomething'] && context['doSomething'].bind(owner, {});
+ p1.on['click'] = function (e) {if (handler2) { handler2(e); } else { context.doSomething({}); }};
c1.push({text: \`Click\`});
return vn1;
}"
@@ -1705,10 +1696,8 @@ exports[`t-on can bind handlers with loop variable as argument 1`] = `
let c6 = [], p6 = {key:6,on:{}};
var vn6 = h('a', p6, c6);
c5.push(vn6);
- if (!context['activate']) {
- throw new Error('Missing handler \\\\'' + 'activate' + \`\\\\' when evaluating template 'test'\`)
- }
- p6.on['click'] = context['activate'].bind(owner, context['action']);
+ const handler7 = context['activate'] && context['activate'].bind(owner, context['action']);
+ p6.on['click'] = function (e) {if (handler7) { handler7(e); } else { context.activate(action); }};
c6.push({text: \`link\`});
}
return vn1;
@@ -1722,10 +1711,8 @@ exports[`t-on can bind handlers with object arguments 1`] = `
var h = this.h;
let c1 = [], p1 = {key:1,on:{}};
var vn1 = h('button', p1, c1);
- if (!context['add']) {
- throw new Error('Missing handler \\\\'' + 'add' + \`\\\\' when evaluating template 'test'\`)
- }
- p1.on['click'] = context['add'].bind(owner, {val:5});
+ const handler2 = context['add'] && context['add'].bind(owner, {val:5});
+ p1.on['click'] = function (e) {if (handler2) { handler2(e); } else { context.add({val: 5}); }};
c1.push({text: \`Click\`});
return vn1;
}"
@@ -1738,15 +1725,9 @@ exports[`t-on can bind two event handlers 1`] = `
var h = this.h;
let c1 = [], p1 = {key:1,on:{}};
var vn1 = h('button', p1, c1);
- if (!context['handleClick']) {
- throw new Error('Missing handler \\\\'' + 'handleClick' + \`\\\\' when evaluating template 'test'\`)
- }
- extra.handlers['click' + 1] = extra.handlers['click' + 1] || context['handleClick'].bind(owner);
+ extra.handlers['click' + 1] = extra.handlers['click' + 1] || function (e) {const fn = context['handleClick'];if (fn) { fn.call(owner, e); } else { context.handleClick; }};
p1.on['click'] = extra.handlers['click' + 1];
- if (!context['handleDblClick']) {
- throw new Error('Missing handler \\\\'' + 'handleDblClick' + \`\\\\' when evaluating template 'test'\`)
- }
- extra.handlers['dblclick' + 1] = extra.handlers['dblclick' + 1] || context['handleDblClick'].bind(owner);
+ extra.handlers['dblclick' + 1] = extra.handlers['dblclick' + 1] || function (e) {const fn = context['handleDblClick'];if (fn) { fn.call(owner, e); } else { context.handleDblClick; }};
p1.on['dblclick'] = extra.handlers['dblclick' + 1];
c1.push({text: \`Click\`});
return vn1;
@@ -1760,10 +1741,7 @@ exports[`t-on handler is bound to proper owner 1`] = `
var h = this.h;
let c1 = [], p1 = {key:1,on:{}};
var vn1 = h('button', p1, c1);
- if (!context['add']) {
- throw new Error('Missing handler \\\\'' + 'add' + \`\\\\' when evaluating template 'test'\`)
- }
- extra.handlers['click' + 1] = extra.handlers['click' + 1] || context['add'].bind(owner);
+ extra.handlers['click' + 1] = extra.handlers['click' + 1] || function (e) {const fn = context['add'];if (fn) { fn.call(owner, e); } else { context.add; }};
p1.on['click'] = extra.handlers['click' + 1];
c1.push({text: \`Click\`});
return vn1;
@@ -1780,10 +1758,7 @@ exports[`t-on t-on combined with t-esc 1`] = `
let c2 = [], p2 = {key:2,on:{}};
var vn2 = h('button', p2, c2);
c1.push(vn2);
- if (!context['onClick']) {
- throw new Error('Missing handler \\\\'' + 'onClick' + \`\\\\' when evaluating template 'test'\`)
- }
- extra.handlers['click' + 2] = extra.handlers['click' + 2] || context['onClick'].bind(owner);
+ extra.handlers['click' + 2] = extra.handlers['click' + 2] || function (e) {const fn = context['onClick'];if (fn) { fn.call(owner, e); } else { context.onClick; }};
p2.on['click'] = extra.handlers['click' + 2];
var _3 = context['text'];
if (_3 || _3 === 0) {
@@ -1804,10 +1779,7 @@ exports[`t-on t-on combined with t-raw 1`] = `
let c2 = [], p2 = {key:2,on:{}};
var vn2 = h('button', p2, c2);
c1.push(vn2);
- if (!context['onClick']) {
- throw new Error('Missing handler \\\\'' + 'onClick' + \`\\\\' when evaluating template 'test'\`)
- }
- extra.handlers['click' + 2] = extra.handlers['click' + 2] || context['onClick'].bind(owner);
+ extra.handlers['click' + 2] = extra.handlers['click' + 2] || function (e) {const fn = context['onClick'];if (fn) { fn.call(owner, e); } else { context.onClick; }};
p2.on['click'] = extra.handlers['click' + 2];
var _3 = context['html'];
if (_3 || _3 === 0) {
@@ -1817,6 +1789,34 @@ exports[`t-on t-on combined with t-raw 1`] = `
}"
`;
+exports[`t-on t-on with inline statement (function call) 1`] = `
+"function anonymous(context,extra
+) {
+ let owner = context;
+ var h = this.h;
+ let c1 = [], p1 = {key:1,on:{}};
+ var vn1 = h('button', p1, c1);
+ const handler2 = context['state.incrementCounter'] && context['state.incrementCounter'].bind(owner, 2);
+ p1.on['click'] = function (e) {if (handler2) { handler2(e); } else { context.state.incrementCounter(2); }};
+ c1.push({text: \`Click\`});
+ return vn1;
+}"
+`;
+
+exports[`t-on t-on with inline statement 1`] = `
+"function anonymous(context,extra
+) {
+ let owner = context;
+ var h = this.h;
+ let c1 = [], p1 = {key:1,on:{}};
+ var vn1 = h('button', p1, c1);
+ extra.handlers['click' + 1] = extra.handlers['click' + 1] || function (e) {const fn = context['state.counter++'];if (fn) { fn.call(owner, e); } else { context.state.counter++; }};
+ p1.on['click'] = extra.handlers['click' + 1];
+ c1.push({text: \`Click\`});
+ return vn1;
+}"
+`;
+
exports[`t-on t-on with prevent and self modifiers (order matters) 1`] = `
"function anonymous(context,extra
) {
@@ -1827,10 +1827,7 @@ exports[`t-on t-on with prevent and self modifiers (order matters) 1`] = `
let c2 = [], p2 = {key:2,on:{}};
var vn2 = h('button', p2, c2);
c1.push(vn2);
- if (!context['onClick']) {
- throw new Error('Missing handler \\\\'' + 'onClick' + \`\\\\' when evaluating template 'test'\`)
- }
- extra.handlers['click' + 2] = extra.handlers['click' + 2] || function (e) {e.preventDefault();if (e.target !== this.elm) {return}context['onClick'].call(owner, e);};
+ extra.handlers['click' + 2] = extra.handlers['click' + 2] || function (e) {e.preventDefault();if (e.target !== this.elm) {return}const fn = context['onClick'];if (fn) { fn.call(owner, e); } else { context.onClick; }};
p2.on['click'] = extra.handlers['click' + 2];
let c3 = [], p3 = {key:3};
var vn3 = h('span', p3, c3);
@@ -1850,28 +1847,19 @@ exports[`t-on t-on with prevent and/or stop modifiers 1`] = `
let c2 = [], p2 = {key:2,on:{}};
var vn2 = h('button', p2, c2);
c1.push(vn2);
- if (!context['onClickPrevented']) {
- throw new Error('Missing handler \\\\'' + 'onClickPrevented' + \`\\\\' when evaluating template 'test'\`)
- }
- extra.handlers['click' + 2] = extra.handlers['click' + 2] || function (e) {e.preventDefault();context['onClickPrevented'].call(owner, e);};
+ extra.handlers['click' + 2] = extra.handlers['click' + 2] || function (e) {e.preventDefault();const fn = context['onClickPrevented'];if (fn) { fn.call(owner, e); } else { context.onClickPrevented; }};
p2.on['click'] = extra.handlers['click' + 2];
c2.push({text: \`Button 1\`});
let c3 = [], p3 = {key:3,on:{}};
var vn3 = h('button', p3, c3);
c1.push(vn3);
- if (!context['onClickStopped']) {
- throw new Error('Missing handler \\\\'' + 'onClickStopped' + \`\\\\' when evaluating template 'test'\`)
- }
- extra.handlers['click' + 3] = extra.handlers['click' + 3] || function (e) {e.stopPropagation();context['onClickStopped'].call(owner, e);};
+ extra.handlers['click' + 3] = extra.handlers['click' + 3] || function (e) {e.stopPropagation();const fn = context['onClickStopped'];if (fn) { fn.call(owner, e); } else { context.onClickStopped; }};
p3.on['click'] = extra.handlers['click' + 3];
c3.push({text: \`Button 2\`});
let c4 = [], p4 = {key:4,on:{}};
var vn4 = h('button', p4, c4);
c1.push(vn4);
- if (!context['onClickPreventedAndStopped']) {
- throw new Error('Missing handler \\\\'' + 'onClickPreventedAndStopped' + \`\\\\' when evaluating template 'test'\`)
- }
- extra.handlers['click' + 4] = extra.handlers['click' + 4] || function (e) {e.preventDefault();e.stopPropagation();context['onClickPreventedAndStopped'].call(owner, e);};
+ extra.handlers['click' + 4] = extra.handlers['click' + 4] || function (e) {e.preventDefault();e.stopPropagation();const fn = context['onClickPreventedAndStopped'];if (fn) { fn.call(owner, e); } else { context.onClickPreventedAndStopped; }};
p4.on['click'] = extra.handlers['click' + 4];
c4.push({text: \`Button 3\`});
return vn1;
@@ -1905,11 +1893,8 @@ exports[`t-on t-on with prevent modifier in t-foreach 1`] = `
let c6 = [], p6 = {key:nodeKey6,attrs:{href: _5},on:{}};
var vn6 = h('a', p6, c6);
c1.push(vn6);
- if (!context['onEdit']) {
- throw new Error('Missing handler \\\\'' + 'onEdit' + \`\\\\' when evaluating template 'test'\`)
- }
- const handler7 = context['onEdit'].bind(owner, context['project'].id);
- p6.on['click'] = function (e) {e.preventDefault();handler7(e);};
+ const handler7 = context['onEdit'] && context['onEdit'].bind(owner, context['project'].id);
+ p6.on['click'] = function (e) {e.preventDefault();if (handler7) { handler7(e); } else { context.onEdit(project.id); }};
c6.push({text: \` Edit \`});
var _8 = context['project'].name;
if (_8 || _8 === 0) {
@@ -1930,10 +1915,7 @@ exports[`t-on t-on with self and prevent modifiers (order matters) 1`] = `
let c2 = [], p2 = {key:2,on:{}};
var vn2 = h('button', p2, c2);
c1.push(vn2);
- if (!context['onClick']) {
- throw new Error('Missing handler \\\\'' + 'onClick' + \`\\\\' when evaluating template 'test'\`)
- }
- extra.handlers['click' + 2] = extra.handlers['click' + 2] || function (e) {if (e.target !== this.elm) {return}e.preventDefault();context['onClick'].call(owner, e);};
+ extra.handlers['click' + 2] = extra.handlers['click' + 2] || function (e) {if (e.target !== this.elm) {return}e.preventDefault();const fn = context['onClick'];if (fn) { fn.call(owner, e); } else { context.onClick; }};
p2.on['click'] = extra.handlers['click' + 2];
let c3 = [], p3 = {key:3};
var vn3 = h('span', p3, c3);
@@ -1953,10 +1935,7 @@ exports[`t-on t-on with self modifier 1`] = `
let c2 = [], p2 = {key:2,on:{}};
var vn2 = h('button', p2, c2);
c1.push(vn2);
- if (!context['onClick']) {
- throw new Error('Missing handler \\\\'' + 'onClick' + \`\\\\' when evaluating template 'test'\`)
- }
- extra.handlers['click' + 2] = extra.handlers['click' + 2] || context['onClick'].bind(owner);
+ extra.handlers['click' + 2] = extra.handlers['click' + 2] || function (e) {const fn = context['onClick'];if (fn) { fn.call(owner, e); } else { context.onClick; }};
p2.on['click'] = extra.handlers['click' + 2];
let c3 = [], p3 = {key:3};
var vn3 = h('span', p3, c3);
@@ -1965,10 +1944,7 @@ exports[`t-on t-on with self modifier 1`] = `
let c4 = [], p4 = {key:4,on:{}};
var vn4 = h('button', p4, c4);
c1.push(vn4);
- if (!context['onClickSelf']) {
- throw new Error('Missing handler \\\\'' + 'onClickSelf' + \`\\\\' when evaluating template 'test'\`)
- }
- extra.handlers['click' + 4] = extra.handlers['click' + 4] || function (e) {if (e.target !== this.elm) {return}context['onClickSelf'].call(owner, e);};
+ extra.handlers['click' + 4] = extra.handlers['click' + 4] || function (e) {if (e.target !== this.elm) {return}const fn = context['onClickSelf'];if (fn) { fn.call(owner, e); } else { context.onClickSelf; }};
p4.on['click'] = extra.handlers['click' + 4];
let c5 = [], p5 = {key:5};
var vn5 = h('span', p5, c5);
diff --git a/tests/qweb/qweb.test.ts b/tests/qweb/qweb.test.ts
index 5f3ae2a1..50d9ccb1 100644
--- a/tests/qweb/qweb.test.ts
+++ b/tests/qweb/qweb.test.ts
@@ -85,13 +85,6 @@ describe("error handling", () => {
}).toThrow("Invalid XML in template");
});
- test("nice error when t-on-directive is evaluated with a missing handler", () => {
- qweb.addTemplate("templatename", ``);
- expect(() => qweb.render("templatename", {}, { handlers: [] })).toThrow(
- "Missing handler 'somemethod' when evaluating template 'templatename'"
- );
- });
-
test("nice error when t-on is evaluated with a missing event", () => {
qweb.addTemplate("templatename", ``);
expect(() => qweb.render("templatename", { someMethod() {} }, { handlers: [] })).toThrow(
@@ -340,41 +333,49 @@ describe("t-if", () => {
});
test("t-set, then t-if", () => {
- qweb.addTemplate("test", `
+ qweb.addTemplate(
+ "test",
+ `
-
`);
+ `
+ );
const result = renderToString(qweb, "test");
const expected = `test
`;
expect(result).toBe(expected);
});
test("t-set, then t-if, part 2", () => {
- qweb.addTemplate("test", `
+ qweb.addTemplate(
+ "test",
+ `
COUCOU
-
`);
+ `
+ );
const result = renderToString(qweb, "test");
const expected = `COUCOU
`;
expect(result).toBe(expected);
});
test("t-set, then t-elif, part 3", () => {
- qweb.addTemplate("test", `
+ qweb.addTemplate(
+ "test",
+ `
AAA
BBB
-
`);
+ `
+ );
const result = renderToString(qweb, "test");
const expected = `BBB
`;
expect(result).toBe(expected);
});
-
});
describe("attributes", () => {
@@ -1039,6 +1040,33 @@ describe("t-on", () => {
(node).click();
});
+ test("t-on with inline statement", () => {
+ qweb.addTemplate("test", ``);
+ let owner = {
+ state: {
+ counter: 0,
+ },
+ };
+ const node = renderToDOM(qweb, "test", owner, { handlers: [] });
+ expect(owner.state.counter).toBe(0);
+ (node).click();
+ expect(owner.state.counter).toBe(1);
+ });
+
+ test("t-on with inline statement (function call)", () => {
+ qweb.addTemplate("test", ``);
+ let owner = {
+ state: {
+ counter: 0,
+ incrementCounter: (inc) => { owner.state.counter += inc; },
+ },
+ };
+ const node = renderToDOM(qweb, "test", owner, { handlers: [] });
+ expect(owner.state.counter).toBe(0);
+ (node).click();
+ expect(owner.state.counter).toBe(2);
+ });
+
test("t-on with prevent and/or stop modifiers", async () => {
expect.assertions(7);
qweb.addTemplate(
diff --git a/tests/router/__snapshots__/link.test.ts.snap b/tests/router/__snapshots__/link.test.ts.snap
index 85868386..7bdbabe7 100644
--- a/tests/router/__snapshots__/link.test.ts.snap
+++ b/tests/router/__snapshots__/link.test.ts.snap
@@ -10,10 +10,7 @@ exports[`Link component can render simple cases 1`] = `
var _2 = context['href'];
let c3 = [], p3 = {key:3,attrs:{href: _2},class:_1,on:{}};
var vn3 = h('a', p3, c3);
- if (!context['navigate']) {
- throw new Error('Missing handler \\\\'' + 'navigate' + \`\\\\' when evaluating template '__template__1'\`)
- }
- extra.handlers['click' + 3] = extra.handlers['click' + 3] || context['navigate'].bind(owner);
+ extra.handlers['click' + 3] = extra.handlers['click' + 3] || function (e) {const fn = context['navigate'];if (fn) { fn.call(owner, e); } else { context.navigate; }};
p3.on['click'] = extra.handlers['click' + 3];
const slot4 = this.constructor.slots[context.__owl__.slotId + '_' + 'default'];
if (slot4) {