-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpattern.html
55 lines (42 loc) · 913 Bytes
/
pattern.html
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
<html>
<body>
<button>countUp</button>
<div></div>
<script>
// Action
const COUNT_UP = "COUNT_UP";
function countUp() {
return {
type: COUNT_UP
};
}
// Reducer
function reducer(state, action) {
switch (action.type) {
case COUNT_UP:
return state + 1;
default:
return state
}
}
// State Store
let store = 0; // Initial state
document.addEventListener('action', function(e) {
store = reducer(store, e.detail);
document.dispatchEvent(new CustomEvent('state'));
}, false);
// UI state change method
function render() {
let $div = document.querySelector('div');
$div.innerHTML = store;
}
document.addEventListener('state', render);
render();
// UI event handler
let $button = document.querySelector('button');
$button.addEventListener("click", function() {
document.dispatchEvent(
new CustomEvent('action', { detail: countUp()})
);
});
</script>