add inlineTemplate key to widget

This commit is contained in:
Géry Debongnie
2019-02-03 09:42:35 +01:00
parent fdd11907fb
commit bf7070016d
3 changed files with 55 additions and 115 deletions
+7 -1
View File
@@ -43,6 +43,7 @@ export interface Type<T> extends Function {
export class Widget<T extends WEnv, Props> extends EventBus {
__widget__: Meta<WEnv>;
template: string = "default";
inlineTemplate: string | null = null;
get el(): HTMLElement | null {
return this.__widget__.vnode ? (<any>this).__widget__.vnode.elm : null;
@@ -60,6 +61,7 @@ export class Widget<T extends WEnv, Props> extends EventBus {
constructor(parent: Widget<T, any> | T, props?: Props) {
super();
wl.push(this);
// is this a good idea?
// Pro: if props is empty, we can create easily a widget
// Con: this is not really safe
@@ -210,11 +212,15 @@ export class Widget<T extends WEnv, Props> extends EventBus {
if (!this.__widget__.isDestroyed) {
this.__widget__.isStarted = true;
}
if (this.inlineTemplate) {
this.env.qweb.addTemplate(this.inlineTemplate, this.inlineTemplate, true);
}
}
private async _render(): Promise<VNode> {
const promises: Promise<void>[] = [];
let vnode = this.env.qweb.render(this.template, this, { promises });
const template = this.inlineTemplate || this.template;
let vnode = this.env.qweb.render(template, this, { promises });
// this part is critical for the patching process to be done correctly. The
// tricky part is that a child widget can be rerendered on its own, which
+10 -2
View File
@@ -132,9 +132,17 @@ export class QWeb {
* Add a template to the internal template map. Note that it is not
* immediately compiled.
*/
addTemplate(name: string, template: RawTemplate) {
addTemplate(
name: string,
template: RawTemplate,
allowDuplicates: boolean = false
) {
if (name in this.processedTemplates) {
throw new Error(`Template ${name} already defined`);
if (allowDuplicates) {
return;
} else {
throw new Error(`Template ${name} already defined`);
}
}
const parser = new DOMParser();
const doc = parser.parseFromString(template, "text/xml");