Skip to content

Commit 0f9dfa6

Browse files
[IMP] awesome_owl: use callback to add counters
define a new state in the playground to hold the summation of the two children counters. bind the onChange function to increase the sum by one to define the context for the function. define the optional prop as an onChange function that is called when the child's state is changed to update the parent's sum. chapter-1-part-6
1 parent 655c35b commit 0f9dfa6

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

awesome_owl/static/src/counter/counter.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ import { Component, useState } from '@odoo/owl';
22

33
export class Counter extends Component {
44
static template = 'awesome_owl.Counter';
5-
static props = {};
5+
static props = {
6+
onChange: {
7+
type: Function,
8+
optional: true,
9+
},
10+
};
611

712
setup() {
813
this.state = useState({ value: 0 });
914
}
1015

1116
increment() {
1217
this.state.value++;
18+
this.props.onChange && this.props.onChange();
1319
}
1420
}

awesome_owl/static/src/playground.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** @odoo-module **/
22

3-
import { Component, markup } from '@odoo/owl';
3+
import { Component, markup, useState } from '@odoo/owl';
44
import { Counter } from './counter/counter';
55
import { Card } from './card/card';
66

@@ -12,5 +12,13 @@ export class Playground extends Component {
1212
setup() {
1313
this.escapedValue = "Hello, <b>world</b>!";
1414
this.notEscapedValue = markup("Hello, <b>world</b>!");
15+
this.countersSum = useState({
16+
value: 0,
17+
});
18+
this.increaseSum = this.increaseSum.bind(this);
19+
}
20+
21+
increaseSum() {
22+
this.countersSum.value++;
1523
}
1624
}

awesome_owl/static/src/playground.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
hello world
66
</div>
77
<div>
8-
<Counter />
9-
<Counter />
8+
<div class="p-3 m-3 border">
9+
<Counter onChange="increaseSum" />
10+
<Counter onChange="increaseSum" />
11+
<p>Counters Sum: <t t-esc="countersSum.value" /></p>
12+
</div>
1013
<br />
1114
<Card title="'another title'" content="escapedValue" />
1215
<Card title="'yet another title'" content="notEscapedValue" />

0 commit comments

Comments
 (0)