-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathapp.js
More file actions
47 lines (39 loc) · 1.24 KB
/
app.js
File metadata and controls
47 lines (39 loc) · 1.24 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
var Storage = require('simple-local-storage')
var history = require('sheet-router/history')
var createApp = require('virtual-app')
var vdom = require('virtual-dom')
var xtend = require('xtend')
var url = require('url')
var ActionCreators = require('./actions')
var initialState = require('./lib/initial-state')
var modifier = require('./modifiers')
var store = new Storage()
var config = require('./config')
var app = createApp(vdom)
var state = xtend({}, store.get(config.slug) || initialState)
var render = app.start(modifier, state)
var router = require('./lib/router')(app)
app.router = router
var actions = ActionCreators(app)
var appEl = document.querySelector('#app')
var href
var tree = render(function (state) {
href = href || url.parse(window.location.href).pathname
if (!state.ui) state.ui = xtend({}, initialState.ui)
var props = xtend({}, state, {
store: app.store,
actions: actions
})
return router(href)(xtend({}, props))
})
app.on('*', function (action, state, oldState) {
var storedState = xtend({}, state)
delete storedState.ui
store.set(config.slug, storedState)
})
history(function (href) {
href = href || url.parse(window.location.href).pathname
actions.setRoute(href)
})
appEl.appendChild(tree)
module.exports = app