[REF] component: move template check in constructor

This commit is contained in:
Géry Debongnie
2019-10-07 17:01:55 +02:00
committed by Aaron Bohy
parent e7516be95c
commit cedb1f411f
3 changed files with 31 additions and 17 deletions
+18 -15
View File
@@ -81,7 +81,7 @@ interface Internal<T extends Env, Props> {
boundHandlers: { [key: number]: any }; boundHandlers: { [key: number]: any };
observer: Observer | null; observer: Observer | null;
render: CompiledTemplate | null; render: CompiledTemplate;
mountedCB: Function | null; mountedCB: Function | null;
willUnmountCB: Function | null; willUnmountCB: Function | null;
willPatchCB: Function | null; willPatchCB: Function | null;
@@ -176,6 +176,8 @@ export class Component<T extends Env, Props extends {}> {
} }
}); });
} }
const qweb = this.env.qweb;
this.__owl__ = { this.__owl__ = {
id: id, id: id,
vnode: null, vnode: null,
@@ -191,7 +193,7 @@ export class Component<T extends Env, Props extends {}> {
willPatchCB: null, willPatchCB: null,
patchedCB: null, patchedCB: null,
observer: null, observer: null,
render: null, render: qweb.render.bind(qweb, this.__getTemplate(qweb)),
classObj: null, classObj: null,
refs: null refs: null
}; };
@@ -538,18 +540,7 @@ export class Component<T extends Env, Props extends {}> {
return fiber.promise; return fiber.promise;
} }
async __prepareAndRender(fiber: Fiber<Props>): Promise<VNode> { __getTemplate(qweb: QWeb): string {
try {
await this.willStart();
} catch (e) {
errorHandler(e, this);
return Promise.resolve(h("div"));
}
const __owl__ = this.__owl__;
if (__owl__.isDestroyed) {
return Promise.resolve(h("div"));
}
const qweb = this.env.qweb;
let p = (<any>this).constructor; let p = (<any>this).constructor;
// console.warn(p, p.template, p._template, 'template' in p, p.hasOwnProperty('template')) // console.warn(p, p.template, p._template, 'template' in p, p.hasOwnProperty('template'))
if (!p.hasOwnProperty("_template")) { if (!p.hasOwnProperty("_template")) {
@@ -571,7 +562,19 @@ export class Component<T extends Env, Props extends {}> {
} }
} }
} }
__owl__.render = qweb.render.bind(qweb, p._template); return p._template;
}
async __prepareAndRender(fiber: Fiber<Props>): Promise<VNode> {
try {
await this.willStart();
} catch (e) {
errorHandler(e, this);
return Promise.resolve(h("div"));
}
const __owl__ = this.__owl__;
if (__owl__.isDestroyed) {
return Promise.resolve(h("div"));
}
return this.__render(fiber); return this.__render(fiber);
} }
+1 -2
View File
@@ -89,9 +89,8 @@ describe("basic widget properties", () => {
test("crashes if it cannot find a template", async () => { test("crashes if it cannot find a template", async () => {
expect.assertions(1); expect.assertions(1);
class SomeWidget extends Component<any, any> {} class SomeWidget extends Component<any, any> {}
const widget = new SomeWidget(env);
try { try {
await widget.mount(fixture); new SomeWidget(env);
} catch (e) { } catch (e) {
expect(e.message).toBe('Could not find template for component "SomeWidget"'); expect(e.message).toBe('Could not find template for component "SomeWidget"');
} }
+12
View File
@@ -1,6 +1,7 @@
import { Component, Env } from "../../src/component/component"; import { Component, Env } from "../../src/component/component";
import { makeTestFixture, makeTestEnv } from "../helpers"; import { makeTestFixture, makeTestEnv } from "../helpers";
import { QWeb } from "../../src/qweb"; import { QWeb } from "../../src/qweb";
import { xml } from "../../src/tags";
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Setup and helpers // Setup and helpers
@@ -31,6 +32,7 @@ describe("props validation", () => {
test("validation is only done in dev mode", async () => { test("validation is only done in dev mode", async () => {
class TestWidget extends Widget { class TestWidget extends Widget {
static props = ["message"]; static props = ["message"];
static template = xml`<div>hey</div>`;
} }
QWeb.dev = true; QWeb.dev = true;
@@ -48,6 +50,7 @@ describe("props validation", () => {
test("props: list of strings", async () => { test("props: list of strings", async () => {
class TestWidget extends Widget { class TestWidget extends Widget {
static props = ["message"]; static props = ["message"];
static template = xml`<div>hey</div>`;
} }
expect(() => { expect(() => {
@@ -67,6 +70,7 @@ describe("props validation", () => {
for (let test of Tests) { for (let test of Tests) {
let TestWidget = class extends Widget { let TestWidget = class extends Widget {
static template = xml`<div>hey</div>`;
static props = { p: test.type }; static props = { p: test.type };
}; };
@@ -96,6 +100,7 @@ describe("props validation", () => {
for (let test of Tests) { for (let test of Tests) {
let TestWidget = class extends Widget { let TestWidget = class extends Widget {
static template = xml`<div>hey</div>`;
static props = { p: { type: test.type } }; static props = { p: { type: test.type } };
}; };
@@ -115,6 +120,7 @@ describe("props validation", () => {
test("can validate a prop with multiple types", async () => { test("can validate a prop with multiple types", async () => {
let TestWidget = class extends Widget { let TestWidget = class extends Widget {
static template = xml`<div>hey</div>`;
static props = { p: [String, Boolean] }; static props = { p: [String, Boolean] };
}; };
@@ -130,6 +136,7 @@ describe("props validation", () => {
test("can validate an optional props", async () => { test("can validate an optional props", async () => {
let TestWidget = class extends Widget { let TestWidget = class extends Widget {
static template = xml`<div>hey</div>`;
static props = { p: { type: String, optional: true } }; static props = { p: { type: String, optional: true } };
}; };
@@ -145,6 +152,7 @@ describe("props validation", () => {
test("can validate an array with given primitive type", async () => { test("can validate an array with given primitive type", async () => {
let TestWidget = class extends Widget { let TestWidget = class extends Widget {
static template = xml`<div>hey</div>`;
static props = { p: { type: Array, element: String } }; static props = { p: { type: Array, element: String } };
}; };
@@ -164,6 +172,7 @@ describe("props validation", () => {
test("can validate an array with multiple sub element types", async () => { test("can validate an array with multiple sub element types", async () => {
let TestWidget = class extends Widget { let TestWidget = class extends Widget {
static template = xml`<div>hey</div>`;
static props = { p: { type: Array, element: [String, Boolean] } }; static props = { p: { type: Array, element: [String, Boolean] } };
}; };
@@ -180,6 +189,7 @@ describe("props validation", () => {
test("can validate an object with simple shape", async () => { test("can validate an object with simple shape", async () => {
let TestWidget = class extends Widget { let TestWidget = class extends Widget {
static template = xml`<div>hey</div>`;
static props = { static props = {
p: { type: Object, shape: { id: Number, url: String } } p: { type: Object, shape: { id: Number, url: String } }
}; };
@@ -201,6 +211,7 @@ describe("props validation", () => {
test("can validate recursively complicated prop def", async () => { test("can validate recursively complicated prop def", async () => {
let TestWidget = class extends Widget { let TestWidget = class extends Widget {
static template = xml`<div>hey</div>`;
static props = { static props = {
p: { p: {
type: Object, type: Object,
@@ -316,6 +327,7 @@ describe("default props", () => {
test("can set default values", async () => { test("can set default values", async () => {
class TestWidget extends Widget { class TestWidget extends Widget {
static defaultProps = { p: 4 }; static defaultProps = { p: 4 };
static template = xml`<div>hey</div>`;
} }
const w = new TestWidget(env, {}); const w = new TestWidget(env, {});