[IMP] qweb: add global Component registry to qweb

Closes #112
This commit is contained in:
Aaron Bohy
2019-05-23 16:14:52 +02:00
committed by Géry Debongnie
parent 24c569e6d5
commit 7cdd1aa9a1
6 changed files with 73 additions and 9 deletions
+12
View File
@@ -132,6 +132,7 @@ let nextID = 1;
export class QWeb {
templates: { [name: string]: Template } = {};
utils = UTILS;
static widgets = Object.create(null);
// dev mode enables better error messages or more costly validations
static dev: boolean = false;
@@ -157,6 +158,13 @@ export class QWeb {
}
}
static register(name: string, Component: any) {
if (QWeb.widgets[name]) {
throw new Error(`Component '${name}' has already been registered`);
}
QWeb.widgets[name] = Component;
}
/**
* Add a template to the internal template map. Note that it is not
* immediately compiled.
@@ -269,6 +277,9 @@ export class QWeb {
// pollute the rendering context by adding some keys in it.
ctx.code.unshift(" let owner = context;");
}
if (ctx.shouldDefineQWeb) {
ctx.code.unshift(" let QWeb = this.constructor;");
}
if (ctx.shouldDefineUtils) {
ctx.code.unshift(" let utils = this.utils;");
}
@@ -616,6 +627,7 @@ export class Context {
rootContext: Context;
caller: Element | undefined;
shouldDefineOwner: boolean = false;
shouldDefineQWeb: boolean = false;
shouldDefineUtils: boolean = false;
shouldProtectContext: boolean = false;
inLoop: boolean = false;
+4 -1
View File
@@ -343,6 +343,7 @@ QWeb.addDirective({
atNodeEncounter({ ctx, value, node }): boolean {
ctx.addLine("//WIDGET");
ctx.rootContext.shouldDefineOwner = true;
ctx.rootContext.shouldDefineQWeb = true;
ctx.rootContext.shouldDefineUtils = true;
let props = node.getAttribute("t-props");
let keepAlive = node.getAttribute("t-keepalive") ? true : false;
@@ -458,7 +459,9 @@ QWeb.addDirective({
ctx.addIf(`!w${widgetID}`);
// new widget
ctx.addLine(`let W${widgetID} = context.widgets['${value}'];`);
ctx.addLine(
`let W${widgetID} = context.widgets && context.widgets['${value}'] || QWeb.widgets['${value}'];`
);
// maybe only do this in dev mode...
ctx.addLine(