mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
add on-directive
This commit is contained in:
+24
-2
@@ -95,7 +95,8 @@ export default class QWeb {
|
||||
elseDirective,
|
||||
elifDirective,
|
||||
ifDirective,
|
||||
callDirective
|
||||
callDirective,
|
||||
onDirective
|
||||
].forEach(d => this.addDirective(d));
|
||||
}
|
||||
|
||||
@@ -170,6 +171,9 @@ export default class QWeb {
|
||||
* @param {string} name the template should already have been added
|
||||
*/
|
||||
render(name: string, context: any = {}): VNode {
|
||||
if (!(name in this.rawTemplates)) {
|
||||
throw new Error(`Template ${name} does not exist`);
|
||||
}
|
||||
const template = this.templates[name] || this._compile(name);
|
||||
return template(context);
|
||||
}
|
||||
@@ -617,4 +621,22 @@ const forEachDirective: Directive = {
|
||||
ctx.addLine("}");
|
||||
return true;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
const onDirective: Directive = {
|
||||
name: "on",
|
||||
priority: 90,
|
||||
atNodeCreation({ ctx, fullName, value, nodeID }) {
|
||||
const eventName = fullName.slice(5);
|
||||
let extraArgs;
|
||||
let handler = value.replace(/\(.*\)/, function(args) {
|
||||
extraArgs = args.slice(1, -1);
|
||||
return "";
|
||||
});
|
||||
ctx.addLine(
|
||||
`p${nodeID}.on = {${eventName}: context['${handler}'].bind(context${
|
||||
extraArgs ? ", " + extraArgs : ""
|
||||
})}`
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user