[IMP] component: add props on root components

This is a partial revert of commit 7d249d6f09.

The reason is that this changes made it much harder to unit test
components.  Before, we could simply instantiate a component
like this:
const my|Comp = new MyComponent(null, props);

and then simply test it.  This commit reestablish that possibility.
This commit is contained in:
Géry Debongnie
2019-11-01 07:58:25 +01:00
parent 83532db48f
commit d74b5a03db
3 changed files with 19 additions and 15 deletions
+5 -1
View File
@@ -443,10 +443,14 @@ const app = new App();
app.mount(document.body); app.mount(document.body);
``` ```
The root component does not have a parent nor props. It will be setup with an The root component does not have a parent nor `props` (see note below). It will be setup with an
[environment](environment.md) (either the `env` defined on its class, or a [environment](environment.md) (either the `env` defined on its class, or a
default empty environment). default empty environment).
Note: a root component can however be given a `props` object in its constructor,
like this: `new App(null, {some: 'object'});`. It will not be a true `props`
object, managed by Owl (so, for example, it will never be updated).
### Composition ### Composition
The example above shows a QWeb template with a sub component. In a template, The example above shows a QWeb template with a sub component. In a template,
+5 -5
View File
@@ -111,13 +111,10 @@ export class Component<T extends Env, Props extends {}> {
* hand. Other components should be created automatically by the framework (with * hand. Other components should be created automatically by the framework (with
* the t-component directive in a template) * the t-component directive in a template)
*/ */
constructor(parent?: Component<T, any>, props?: Props) { constructor(parent?: Component<T, any> | null, props?: Props) {
Component.current = this; Component.current = this;
const id: number = nextId++;
let constr = this.constructor as any; let constr = this.constructor as any;
let depth;
if (parent) {
const defaultProps = constr.defaultProps; const defaultProps = constr.defaultProps;
if (defaultProps) { if (defaultProps) {
props = this.__applyDefaultProps(props, defaultProps); props = this.__applyDefaultProps(props, defaultProps);
@@ -126,6 +123,10 @@ export class Component<T extends Env, Props extends {}> {
if (QWeb.dev) { if (QWeb.dev) {
QWeb.utils.validateProps(constr, this.props); QWeb.utils.validateProps(constr, this.props);
} }
const id: number = nextId++;
let depth;
if (parent) {
this.env = parent.env; this.env = parent.env;
const __powl__ = parent.__owl__; const __powl__ = parent.__owl__;
__powl__.children[id] = this; __powl__.children[id] = this;
@@ -136,7 +137,6 @@ export class Component<T extends Env, Props extends {}> {
if (!this.env.qweb) { if (!this.env.qweb) {
this.env.qweb = new QWeb(); this.env.qweb = new QWeb();
} }
this.props = (undefined as unknown) as Props;
this.env.qweb.on("update", this, () => { this.env.qweb.on("update", this, () => {
if (this.__owl__.isMounted) { if (this.__owl__.isMounted) {
this.render(true); this.render(true);
+3 -3
View File
@@ -71,9 +71,9 @@ class WidgetA extends Widget {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
describe("basic widget properties", () => { describe("basic widget properties", () => {
test("props is not defined on root components", async () => { test("props is set on root components", async () => {
const widget = new Widget(); const widget = new Widget(null, {});
expect(widget.props).toBe(undefined); expect(widget.props).toEqual({});
}); });
test("has no el after creation", async () => { test("has no el after creation", async () => {