[IMP] allow anonymous Component extensions

It is possible that a Component is extended dynamically and if this is
the case, the class that extends it can be anonymous, with property
name=''. If this is the case, current implementation interprets the empty
string to be false so the while loop is terminated without further
scanning the super classes.

In this proposal, we allow anonymous class to be scanned until its
Component ancestor. Basically, the anonymous class assumes the name of
it super.
This commit is contained in:
Joseph Caburnay
2020-03-12 08:25:33 +01:00
committed by Géry Debongnie
parent ae172d42e7
commit 94c8bce810
2 changed files with 12 additions and 2 deletions
+3 -2
View File
@@ -604,9 +604,10 @@ export class Component<Props extends {} = any, T extends Env = Env> {
// key. So we fall back on looking for a template matching its name (or // key. So we fall back on looking for a template matching its name (or
// one of its subclass). // one of its subclass).
let template: string; let template: string = p.name;
while ((template = p.name) && !(template in qweb.templates) && p !== Component) { while (!(template in qweb.templates) && p !== Component) {
p = p.__proto__; p = p.__proto__;
template = p.name;
} }
if (p === Component) { if (p === Component) {
throw new Error(`Could not find template for component "${this.constructor.name}"`); throw new Error(`Could not find template for component "${this.constructor.name}"`);
+9
View File
@@ -3080,6 +3080,15 @@ describe("can deduce template from name", () => {
expect(fixture.innerHTML).toBe("<span>Orval</span>"); expect(fixture.innerHTML).toBe("<span>Orval</span>");
}); });
test("can find template of anonymous component", async () => {
class ABC extends Component {}
const Anonymous = class extends ABC {};
env.qweb.addTemplate("ABC", "<span>Orval</span>");
const def = new Anonymous();
await def.mount(fixture);
expect(fixture.innerHTML).toBe("<span>Orval</span>");
});
test("can find template of parent component, defined by template key", async () => { test("can find template of parent component, defined by template key", async () => {
class ABC extends Component { class ABC extends Component {
static template = "Achel"; static template = "Achel";