-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspa.js
65 lines (64 loc) · 1.64 KB
/
spa.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
62
63
64
65
import {spa} from "../index.js"
window.stop = spa({
node: document.body.querySelector('main'),
routes: {
'/': {},
'/hello/:name': {
template: document.getElementById('view-hello')
},
'/counter/:start': {
template: document.getElementById('view-counter'),
init: ({Params}) => parseInt(Params.start),
register: update => ({
inc: () => update(count => count + 1),
dec: () => update(count => count - 1)
}),
view: (count, events) => ({
count,
...events
})
},
'/todo': {
template: document.getElementById('view-todo'),
init: ({Query}) => ({
value: "",
todos: Query.todos instanceof Array ? Query.todos : []
}),
register: update => ({
NewValue: ev => update(state => ({
...state,
value: ev.target.value
})),
AddTodo: () => update(({todos, value}) => ({
todos: todos.concat(value),
value: ''
}))
}),
view: (state, events) => ({
...state,
...events
})
},
'/clock': {
template: document.getElementById('view-clock'),
init: () => null,
register: (update, dispatch) => ({
init: () => {
update(() => new Date())
setTimeout(() => {dispatch('init')}, 100)
}
}),
view: time => {
console.log('tick')
return !time ? '00:00:00' : [
time.getHours(),
time.getMinutes(),
time.getSeconds()
].map(n => (n < 10 ? '0' : '')+n).join(':')
}
},
'*': {
template: document.getElementById('view-404')
}
}
})