[IMP] component: remove support for inlineTemplate

closes #93
This commit is contained in:
Géry Debongnie
2019-05-15 11:15:08 +02:00
parent 9c1d369e94
commit 0a10bbc023
7 changed files with 473 additions and 344 deletions
+21 -34
View File
@@ -43,7 +43,7 @@ export interface Meta<T extends Env, Props> {
mountedHandlers: { [key: number]: Function };
}
// If a component does not define explicitely a template (or inlineTemplate)
// If a component does not define explicitely a template
// key, it needs to find a template with its name (or a parent's). This is
// qweb dependant, so we need a place to store this information indexed by
// qweb instances.
@@ -61,7 +61,6 @@ export class Component<
> extends EventBus {
readonly __owl__: Meta<Env, Props>;
template?: string;
inlineTemplate?: string;
get el(): HTMLElement | null {
return this.__owl__.vnode ? (<any>this).__owl__.vnode.elm : null;
@@ -388,41 +387,29 @@ export class Component<
const qweb = this.env.qweb;
if (!this.template) {
if (this.inlineTemplate) {
this.env.qweb.addTemplate(
this.inlineTemplate,
this.inlineTemplate,
true
);
// we write on the proto, so any new component of this class will get
// automatically the template key properly setup.
(<any>this).__proto__.template = this.inlineTemplate;
let tmap = TEMPLATE_MAP[qweb.id];
if (!tmap) {
tmap = {};
TEMPLATE_MAP[qweb.id] = tmap;
}
let p = (<any>this).constructor;
let name: string = p.name;
let template = tmap[name];
if (template) {
this.template = template;
} else {
let tmap = TEMPLATE_MAP[qweb.id];
if (!tmap) {
tmap = {};
TEMPLATE_MAP[qweb.id] = tmap;
while (
(template = p.name) &&
!(template in qweb.templates) &&
p !== Component
) {
p = p.__proto__;
}
let p = (<any>this).constructor;
let name: string = p.name;
let template = tmap[name];
if (template) {
this.template = template;
if (p === Component) {
this.template = "default";
} else {
while (
(template = p.name) &&
!(template in qweb.templates) &&
p !== Component
) {
p = p.__proto__;
}
if (p === Component) {
this.template = "default";
} else {
tmap[name] = template;
this.template = template;
}
tmap[name] = template;
this.template = template;
}
}
}
+1 -8
View File
@@ -152,14 +152,7 @@ export class QWeb {
* Add a template to the internal template map. Note that it is not
* immediately compiled.
*/
addTemplate(
name: string,
xmlString: string,
allowDuplicates: boolean = false
) {
if (name in this.templates && allowDuplicates) {
return;
}
addTemplate(name: string, xmlString: string) {
const doc = parseXML(xmlString);
if (!doc.firstChild) {
throw new Error("Invalid template (should not be empty)");
+12 -2
View File
@@ -72,7 +72,7 @@ export class Store extends EventBus {
const func: (...any) => any = entry[1];
this.getters[name] = payload => {
return func({ state: this.state, getters: this.getters }, payload);
}
};
}
}
@@ -154,6 +154,8 @@ interface EnvWithStore extends Env {
store: Store;
}
let nextID = 1;
export function connect(mapStateToProps, options: any = {}) {
let hashFunction = options.hashFunction || null;
@@ -184,7 +186,7 @@ export function connect(mapStateToProps, options: any = {}) {
return function<E extends EnvWithStore, P, S>(
Comp: Constructor<Component<E, P, S>>
) {
return class extends Comp {
const Result = class extends Comp {
constructor(parent, props?: any) {
const env = parent instanceof Component ? parent.env : parent;
const ownProps = Object.assign({}, props || {});
@@ -269,5 +271,13 @@ export function connect(mapStateToProps, options: any = {}) {
return super._updateProps(mergedProps, forceUpdate, patchQueue);
}
};
// we assign here a unique name to the resulting anonymous class.
// this is necessary for Owl to be able to properly deduce templates.
// Otherwise, all connected components would have the same name, and then
// each component after the first will necessarily have the same template.
let name = `ConnectedComponent${nextID++}`;
Object.defineProperty(Result, "name", { value: name });
return Result;
};
}