Skip to content

Commit b343eaa

Browse files
committed
[IMP] add more examples
1 parent 524ed2f commit b343eaa

File tree

16 files changed

+300
-0
lines changed

16 files changed

+300
-0
lines changed

docs/playground/playground.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ const SAMPLES = [
8484
folder: "oca_03",
8585
code: ["js", "xml", "css"],
8686
},
87+
{
88+
description: "OCA 04 - Function Props",
89+
folder: "oca_04",
90+
code: ["js", "xml", "css"],
91+
},
92+
{
93+
description: "OCA 05 - Environment parameters",
94+
folder: "oca_05",
95+
code: ["js", "xml", "css"],
96+
},
97+
{
98+
description: "OCA 06 - Create your hook",
99+
folder: "oca_06",
100+
code: ["js", "xml", "css"],
101+
},
102+
{
103+
description: "OCA 07 - Slots usage",
104+
folder: "oca_07",
105+
code: ["js", "xml", "css"],
106+
},
87107
{
88108
description: "Components",
89109
folder: "components",
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.child {
2+
padding: 2px;
3+
margin: 3px;
4+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// In this example, we show how components can be defined and created.
2+
import { Component, useEnv, useSubEnv, useState, mount } from "@odoo/owl";
3+
4+
export function useCounter() {
5+
const env = useEnv();
6+
if (env.counter) {
7+
return useState(env.counter);
8+
}
9+
const counter = useState({ count: 0 });
10+
useSubEnv({ counter });
11+
return counter;
12+
}
13+
14+
export class Child extends Component {
15+
static template = 'training_oca.Child';
16+
setup() {
17+
this.counter = useCounter();
18+
}
19+
}
20+
21+
export class B extends Component {
22+
static template = 'training_oca.B';
23+
static components = { Child };
24+
}
25+
26+
export class A extends Component {
27+
static template = 'training_oca.A';
28+
static components = { B };
29+
}
30+
31+
export class Parent extends Component {
32+
static template = 'training_oca.Parent';
33+
static components = { A };
34+
setup() {
35+
this.counter = useCounter();
36+
}
37+
onIncrement() {
38+
this.counter.count++;
39+
}
40+
}
41+
42+
mount(Parent, document.body, { templates: TEMPLATES, dev: true });
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<templates xml:space="preserve">
2+
<div t-name="training_oca.Parent">
3+
<button t-on-click="onIncrement">Increment from Child</button>
4+
<A/>
5+
</div>
6+
<t t-name="training_oca.A">
7+
<B/>
8+
</t>
9+
<t t-name="training_oca.B">
10+
<Child/>
11+
<Child/>
12+
<Child/>
13+
</t>
14+
<t t-name="training_oca.Child">
15+
<div class="child" t-esc="counter.count"/>
16+
</t>
17+
</templates>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.child {
2+
padding: 2px;
3+
margin: 3px;
4+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// In this example, we show how components can be defined and created.
2+
import { Component, useState, mount } from "@odoo/owl";
3+
4+
export class Child extends Component {
5+
static template = "training_oca.Child";
6+
static props = { onIncrement: Function };
7+
}
8+
9+
export class Parent extends Component {
10+
static template = "training_oca.Parent";
11+
static components = { Child };
12+
13+
setup() {
14+
this.state = useState({ count: 0 });
15+
}
16+
17+
increment() {
18+
this.state.count++;
19+
}
20+
}
21+
22+
mount(Parent, document.body, { templates: TEMPLATES, dev: true });
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<templates xml:space="preserve">
2+
<t t-name="training_oca.Parent">
3+
<div>
4+
<p>Count: <t t-esc="state.count"/></p>
5+
<Child onIncrement.bind="increment"/>
6+
</div>
7+
</t>
8+
<t t-name="training_oca.Child">
9+
<button t-on-click="props.onIncrement">Increment from Child</button>
10+
</t>
11+
</templates>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.child {
2+
padding: 2px;
3+
margin: 3px;
4+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// In this example, we show how components can be defined and created.
2+
import { Component, useState, mount, useSubEnv } from "@odoo/owl";
3+
4+
export class Child extends Component {
5+
static template = 'training_oca.Child';
6+
setup() {
7+
this.counter = useState(this.env.counter);
8+
}
9+
}
10+
11+
export class B extends Component {
12+
static template = 'training_oca.B';
13+
static components = { Child };
14+
}
15+
16+
export class A extends Component {
17+
static template = 'training_oca.A';
18+
static components = { B };
19+
}
20+
21+
export class Parent extends Component {
22+
static template = 'training_oca.Parent';
23+
static components = { A };
24+
setup() {
25+
this.counter = useState({ count: 0 });
26+
useSubEnv({ counter: this.counter });
27+
}
28+
onIncrement() {
29+
this.counter.count++;
30+
}
31+
}
32+
33+
mount(Parent, document.body, { templates: TEMPLATES, dev: true });
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<templates xml:space="preserve">
2+
<div t-name="training_oca.Parent">
3+
<button t-on-click="onIncrement">Increment from Child</button>
4+
<A/>
5+
</div>
6+
<t t-name="training_oca.A">
7+
<B/>
8+
</t>
9+
<t t-name="training_oca.B">
10+
<Child/>
11+
<Child/>
12+
<Child/>
13+
</t>
14+
<t t-name="training_oca.Child">
15+
<div class="child" t-esc="counter.count"/>
16+
</t>
17+
</templates>

0 commit comments

Comments
 (0)