-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathmulti-wire.html
More file actions
65 lines (52 loc) · 1.51 KB
/
multi-wire.html
File metadata and controls
65 lines (52 loc) · 1.51 KB
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
62
63
64
65
<!doctype html>
<script src="https://unpkg.com/introspected@latest/min.js"></script>
<script src="../min.js"></script>
<script>
this.onnload = function () {
'use strict';
class Foo {
constructor(root) {
this.render = hyperHTML.bind(root);
this.state = Introspected(
{
items: [
{ id: 1, text: 'foo' },
{ id: 2, text: 'bar' },
{ id: 3, text: 'baz' }
]
},
state => this.update(state)
);
// any change to the introspected state will update
this.state.selectedItem = this.state.items[0];
}
// any object with an handleEvent method can be used for any event
// like it is already for DOM Level 3 via addEventListener
handleEvent(e) {
switch (e.type) {
case 'change':
this.state.selectedItem = this.state.items.find(
item => +e.target.value === item.id
);
break;
}
}
update(state) {
this.render`
<select onchange="${this}">${
state.items.map(item => hyperHTML.wire(item, ':option')`
<option value="${item.id}">
${item.text}
</option>
`)}</select>
<p>Selected: ${state.selectedItem.text}</p>
<ul>${state.items.map(item => hyperHTML.wire(item, ':li')`
<li> ${item.id} / ${item.text} </li>
`)}</ul>
`;
}
}
const foo = new Foo(document.body);
setTimeout(() => foo.state.items.push({id: 4, text: 'minions'}), 3000);
};
</script>