-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcounter_vanilla__vanilla.html
86 lines (73 loc) · 2.24 KB
/
counter_vanilla__vanilla.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<!DOCTYPE html>
<html>
<head>
<title>Javscript basic example</title>
<!-- <script src="https://unpkg.com/redux@latest/dist/redux.min.js"></script> // @replace -->
<!-- -->
</head>
<body>
<div>
<p>
Clicked: <span id="value">0</span> times
<button id="increment">+</button>
<button id="decrement">-</button>
<button id="incrementIfOdd">Increment if odd</button>
<button id="incrementAsync">Increment async</button>
</p>
</div>
<script>
function counter(state, action) {
if (typeof state === 'undefined') {
return 0
}
switch (action.type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
}
// var store = Redux.createStore(counter) //@replace
var store = 0;
document.addEventListener('action', function(e) {
store = counter(store, e.detail);
document.dispatchEvent(new CustomEvent('state'));
}, false);
var valueEl = document.getElementById('value')
function render() {
// store.getState().toString() //@replace
valueEl.innerHTML = store.toString()
}
render()
// store.subscribe(render) //@replace
document.addEventListener('state', render);
document.getElementById('increment')
.addEventListener('click', function() {
// store.dispatch({ type: 'INCREMENT' }) //@replace
document.dispatchEvent(new CustomEvent('action', { detail: { type: 'INCREMENT' }}));
})
document.getElementById('decrement')
.addEventListener('click', function() {
// store.dispatch({ type: 'DECREMENT' }) //@replace
document.dispatchEvent(new CustomEvent('action', { detail: { type: 'DECREMENT' }}));
})
document.getElementById('incrementIfOdd')
.addEventListener('click', function() {
// if (store.getState() % 2 !== 0) { // @replace
if (store % 2 !== 0) {
// store.dispatch({ 'INCREMENT' }) //@replace
document.dispatchEvent(new CustomEvent('action', { detail: { type: 'INCREMENT' }}));
}
})
document.getElementById('incrementAsync')
.addEventListener('click', function() {
setTimeout(function() {
// store.dispatch({ 'INCREMENT'}) //@replace
document.dispatchEvent(new CustomEvent('action', { detail: { type: 'INCREMENT' }}));
}, 1000)
})
</script>
</body>
</html>