[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
+4 -2
View File
@@ -4,6 +4,7 @@ exports[`animations t-transition combined with t-widget 1`] = `
"function anonymous(context,extra
) {
let utils = this.utils;
let QWeb = this.constructor;
let owner = context;
var h = this.utils.h;
let c1 = [], p1 = {key:1};
@@ -19,7 +20,7 @@ exports[`animations t-transition combined with t-widget 1`] = `
w4 = false
}
if (!w4) {
let W4 = context.widgets['Child'];
let W4 = context.widgets && context.widgets['Child'] || QWeb.widgets['Child'];
if (!W4) {throw new Error(\`Cannot find the definition of widget \\"Child\\"\`)}
w4 = new W4(owner, props4);
context.__owl__.cmap[4] = w4.__owl__.id;
@@ -41,6 +42,7 @@ exports[`animations t-transition combined with t-widget and t-if 1`] = `
"function anonymous(context,extra
) {
let utils = this.utils;
let QWeb = this.constructor;
let owner = context;
var h = this.utils.h;
let c1 = [], p1 = {key:1};
@@ -57,7 +59,7 @@ exports[`animations t-transition combined with t-widget and t-if 1`] = `
w4 = false
}
if (!w4) {
let W4 = context.widgets['Child'];
let W4 = context.widgets && context.widgets['Child'] || QWeb.widgets['Child'];
if (!W4) {throw new Error(\`Cannot find the definition of widget \\"Child\\"\`)}
w4 = new W4(owner, props4);
context.__owl__.cmap[4] = w4.__owl__.id;
+10 -5
View File
@@ -4,6 +4,7 @@ exports[`class and style attributes with t-widget dynamic t-att-style is properl
"function anonymous(context,extra
) {
let utils = this.utils;
let QWeb = this.constructor;
let owner = context;
var h = this.utils.h;
let c1 = [], p1 = {key:1};
@@ -20,7 +21,7 @@ exports[`class and style attributes with t-widget dynamic t-att-style is properl
w4 = false
}
if (!w4) {
let W4 = context.widgets['child'];
let W4 = context.widgets && context.widgets['child'] || QWeb.widgets['child'];
if (!W4) {throw new Error(\`Cannot find the definition of widget \\"child\\"\`)}
w4 = new W4(owner, props4);
context.__owl__.cmap[4] = w4.__owl__.id;
@@ -39,6 +40,7 @@ exports[`class and style attributes with t-widget t-att-class is properly added/
"function anonymous(context,extra
) {
let utils = this.utils;
let QWeb = this.constructor;
let owner = context;
var h = this.utils.h;
let c1 = [], p1 = {key:1};
@@ -55,7 +57,7 @@ exports[`class and style attributes with t-widget t-att-class is properly added/
w4 = false
}
if (!w4) {
let W4 = context.widgets['child'];
let W4 = context.widgets && context.widgets['child'] || QWeb.widgets['child'];
if (!W4) {throw new Error(\`Cannot find the definition of widget \\"child\\"\`)}
w4 = new W4(owner, props4);
context.__owl__.cmap[4] = w4.__owl__.id;
@@ -78,6 +80,7 @@ exports[`composition sub widgets with some state rendered in a loop 1`] = `
"function anonymous(context,extra
) {
let utils = this.utils;
let QWeb = this.constructor;
let owner = context;
context = Object.create(context);
var h = this.utils.h;
@@ -107,7 +110,7 @@ exports[`composition sub widgets with some state rendered in a loop 1`] = `
w7 = false
}
if (!w7) {
let W7 = context.widgets['ChildWidget'];
let W7 = context.widgets && context.widgets['ChildWidget'] || QWeb.widgets['ChildWidget'];
if (!W7) {throw new Error(\`Cannot find the definition of widget \\"ChildWidget\\"\`)}
w7 = new W7(owner, props7);
context.__owl__.cmap[key8] = w7.__owl__.id;
@@ -127,6 +130,7 @@ exports[`random stuff/miscellaneous snapshotting compiled code 1`] = `
"function anonymous(context,extra
) {
let utils = this.utils;
let QWeb = this.constructor;
let owner = context;
var h = this.utils.h;
let c1 = [], p1 = {key:1};
@@ -143,7 +147,7 @@ exports[`random stuff/miscellaneous snapshotting compiled code 1`] = `
w4 = false
}
if (!w4) {
let W4 = context.widgets['child'];
let W4 = context.widgets && context.widgets['child'] || QWeb.widgets['child'];
if (!W4) {throw new Error(\`Cannot find the definition of widget \\"child\\"\`)}
w4 = new W4(owner, props4);
context.__owl__.cmap[key5] = w4.__owl__.id;
@@ -162,6 +166,7 @@ exports[`random stuff/miscellaneous t-props should not be undefined (snapshottin
"function anonymous(context,extra
) {
let utils = this.utils;
let QWeb = this.constructor;
let owner = context;
var h = this.utils.h;
let c1 = [], p1 = {key:1};
@@ -177,7 +182,7 @@ exports[`random stuff/miscellaneous t-props should not be undefined (snapshottin
w4 = false
}
if (!w4) {
let W4 = context.widgets['child'];
let W4 = context.widgets && context.widgets['child'] || QWeb.widgets['child'];
if (!W4) {throw new Error(\`Cannot find the definition of widget \\"child\\"\`)}
w4 = new W4(owner, props4);
context.__owl__.cmap[4] = w4.__owl__.id;
+25
View File
@@ -1,4 +1,5 @@
import { Component, Env } from "../src/component";
import { QWeb } from "../src/qweb_core";
import {
makeDeferred,
makeTestFixture,
@@ -849,6 +850,30 @@ describe("composition", () => {
expect(children(widget)[0].__owl__.parent).toBe(widget);
});
test("can use widgets from the global registry", async () => {
QWeb.register("WidgetB", WidgetB);
env.qweb.addTemplate("ParentWidget", `<div><t t-widget="WidgetB"/></div>`);
class ParentWidget extends Widget {}
const widget = new ParentWidget(env);
await widget.mount(fixture);
expect(fixture.innerHTML).toBe("<div><div>world</div></div>");
delete QWeb.widgets['WidgetB'];
});
test("don't fallback to global registry if widget defined locally", async () => {
QWeb.register("WidgetB", WidgetB); // should not use this widget
env.qweb.addTemplate("ParentWidget", `<div><t t-widget="WidgetB"/></div>`);
env.qweb.addTemplate("AnotherWidgetB", `<span>Belgium</span>`);
class AnotherWidgetB extends Widget {}
class ParentWidget extends Widget {
widgets = { WidgetB: AnotherWidgetB };
}
const widget = new ParentWidget(env);
await widget.mount(fixture);
expect(fixture.innerHTML).toBe("<div><span>Belgium</span></div>");
delete QWeb.widgets['WidgetB'];
});
test("throw a nice error if it cannot find widget", async () => {
expect.assertions(1);
env.qweb.addTemplate(