-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo2b.js
61 lines (50 loc) · 947 Bytes
/
demo2b.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const { patch, h } = window.snabbdom;
function mount(id, component) {
let el = document.getElementById(id);
const render = component(update);
function update() {
el = patch(el, render());
}
update();
}
function counter(update) {
let num = 0;
function incr(d) {
num += d;
update();
}
return () =>
h('span', { class:{ red: num < 0 }}, [
button('-', [incr, -1]),
" ",
button('+', [incr, 1]),
` Count: ${num} `,
])
}
function button(title, op) {
return h('button', { on:{ click:op }}, title)
}
function counterList(update) {
const counters = [];
function addCounter() {
counters.push(counter(update));
update();
}
function delCounter(i) {
counters.splice(i, 1);
update();
}
return () =>
h('div', [
button('Add', addCounter),
h('ul', counters.map((c, i) =>
h('li', [
button('Del', [delCounter, i]),
' ',
c(),
])
)
),
])
}
mount('cnt1', counterList);