mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[REF] component: move template check in constructor
This commit is contained in:
committed by
Aaron Bohy
parent
e7516be95c
commit
cedb1f411f
+18
-15
@@ -81,7 +81,7 @@ interface Internal<T extends Env, Props> {
|
||||
|
||||
boundHandlers: { [key: number]: any };
|
||||
observer: Observer | null;
|
||||
render: CompiledTemplate | null;
|
||||
render: CompiledTemplate;
|
||||
mountedCB: Function | null;
|
||||
willUnmountCB: Function | null;
|
||||
willPatchCB: Function | null;
|
||||
@@ -176,6 +176,8 @@ export class Component<T extends Env, Props extends {}> {
|
||||
}
|
||||
});
|
||||
}
|
||||
const qweb = this.env.qweb;
|
||||
|
||||
this.__owl__ = {
|
||||
id: id,
|
||||
vnode: null,
|
||||
@@ -191,7 +193,7 @@ export class Component<T extends Env, Props extends {}> {
|
||||
willPatchCB: null,
|
||||
patchedCB: null,
|
||||
observer: null,
|
||||
render: null,
|
||||
render: qweb.render.bind(qweb, this.__getTemplate(qweb)),
|
||||
classObj: null,
|
||||
refs: null
|
||||
};
|
||||
@@ -538,18 +540,7 @@ export class Component<T extends Env, Props extends {}> {
|
||||
return fiber.promise;
|
||||
}
|
||||
|
||||
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"));
|
||||
}
|
||||
const qweb = this.env.qweb;
|
||||
__getTemplate(qweb: QWeb): string {
|
||||
let p = (<any>this).constructor;
|
||||
// console.warn(p, p.template, p._template, 'template' in p, 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);
|
||||
}
|
||||
|
||||
|
||||
@@ -89,9 +89,8 @@ describe("basic widget properties", () => {
|
||||
test("crashes if it cannot find a template", async () => {
|
||||
expect.assertions(1);
|
||||
class SomeWidget extends Component<any, any> {}
|
||||
const widget = new SomeWidget(env);
|
||||
try {
|
||||
await widget.mount(fixture);
|
||||
new SomeWidget(env);
|
||||
} catch (e) {
|
||||
expect(e.message).toBe('Could not find template for component "SomeWidget"');
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Component, Env } from "../../src/component/component";
|
||||
import { makeTestFixture, makeTestEnv } from "../helpers";
|
||||
import { QWeb } from "../../src/qweb";
|
||||
import { xml } from "../../src/tags";
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Setup and helpers
|
||||
@@ -31,6 +32,7 @@ describe("props validation", () => {
|
||||
test("validation is only done in dev mode", async () => {
|
||||
class TestWidget extends Widget {
|
||||
static props = ["message"];
|
||||
static template = xml`<div>hey</div>`;
|
||||
}
|
||||
|
||||
QWeb.dev = true;
|
||||
@@ -48,6 +50,7 @@ describe("props validation", () => {
|
||||
test("props: list of strings", async () => {
|
||||
class TestWidget extends Widget {
|
||||
static props = ["message"];
|
||||
static template = xml`<div>hey</div>`;
|
||||
}
|
||||
|
||||
expect(() => {
|
||||
@@ -67,6 +70,7 @@ describe("props validation", () => {
|
||||
|
||||
for (let test of Tests) {
|
||||
let TestWidget = class extends Widget {
|
||||
static template = xml`<div>hey</div>`;
|
||||
static props = { p: test.type };
|
||||
};
|
||||
|
||||
@@ -96,6 +100,7 @@ describe("props validation", () => {
|
||||
|
||||
for (let test of Tests) {
|
||||
let TestWidget = class extends Widget {
|
||||
static template = xml`<div>hey</div>`;
|
||||
static props = { p: { type: test.type } };
|
||||
};
|
||||
|
||||
@@ -115,6 +120,7 @@ describe("props validation", () => {
|
||||
|
||||
test("can validate a prop with multiple types", async () => {
|
||||
let TestWidget = class extends Widget {
|
||||
static template = xml`<div>hey</div>`;
|
||||
static props = { p: [String, Boolean] };
|
||||
};
|
||||
|
||||
@@ -130,6 +136,7 @@ describe("props validation", () => {
|
||||
|
||||
test("can validate an optional props", async () => {
|
||||
let TestWidget = class extends Widget {
|
||||
static template = xml`<div>hey</div>`;
|
||||
static props = { p: { type: String, optional: true } };
|
||||
};
|
||||
|
||||
@@ -145,6 +152,7 @@ describe("props validation", () => {
|
||||
|
||||
test("can validate an array with given primitive type", async () => {
|
||||
let TestWidget = class extends Widget {
|
||||
static template = xml`<div>hey</div>`;
|
||||
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 () => {
|
||||
let TestWidget = class extends Widget {
|
||||
static template = xml`<div>hey</div>`;
|
||||
static props = { p: { type: Array, element: [String, Boolean] } };
|
||||
};
|
||||
|
||||
@@ -180,6 +189,7 @@ describe("props validation", () => {
|
||||
|
||||
test("can validate an object with simple shape", async () => {
|
||||
let TestWidget = class extends Widget {
|
||||
static template = xml`<div>hey</div>`;
|
||||
static props = {
|
||||
p: { type: Object, shape: { id: Number, url: String } }
|
||||
};
|
||||
@@ -201,6 +211,7 @@ describe("props validation", () => {
|
||||
|
||||
test("can validate recursively complicated prop def", async () => {
|
||||
let TestWidget = class extends Widget {
|
||||
static template = xml`<div>hey</div>`;
|
||||
static props = {
|
||||
p: {
|
||||
type: Object,
|
||||
@@ -316,6 +327,7 @@ describe("default props", () => {
|
||||
test("can set default values", async () => {
|
||||
class TestWidget extends Widget {
|
||||
static defaultProps = { p: 4 };
|
||||
static template = xml`<div>hey</div>`;
|
||||
}
|
||||
|
||||
const w = new TestWidget(env, {});
|
||||
|
||||
Reference in New Issue
Block a user