-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathApp.coffee
62 lines (49 loc) · 2.01 KB
/
App.coffee
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
# ## Entry of the universal app
#
# `App.coffee` contains some methods that can run both on server
# and browser.
React = require 'react'
{ Route, DefaultRoute } = require 'react-router'
{ Provider } = require 'react-redux'
Page = require './components/Page'
Index = require './views/Index'
Todo = require './views/Todo'
{ combineReducers, createStore, applyMiddleware } = require 'redux'
thunk = require 'redux-thunk'
# Here we define our routes. We have two routes: the default route is
# handled by `Index` component and `/todo` route is handled by `Todo` component.
module.exports.routes =
<Route path="/" handler={Page}>
<DefaultRoute handler={Index} />
<Route path="/todo" handler={Todo} />
</Route>
# The method `fetchDataFromRoute` is where all the magic happens.
# Each handler can have a specific static method `initialData()`
# which returns a Promise and fill our reducers with the data required.
# This method returns a `Promise.all` with all the promises required by the
# route handlers.
# When all the data is loaded, we render all the components.
module.exports.fetchDataFromRoute = (state, store) ->
routes = state.routes
promises = routes
# Get all the views who need data
.filter (route) ->
wrapped = route.handler.WrappedComponent
if wrapped and wrapped.initialData then yes
else no
# Return all the promises
.map (route) ->
return route.handler.WrappedComponent.initialData(store)
return Promise.all(promises)
# The method makeHandler connects a Handler with the redux store.
module.exports.makeHandler = (Handler, store) ->
<Provider store={store}>
{ -> <Handler /> }
</Provider>
# This method combines all the reducers into one, add the different
# redux middlewares and hydrate the store with `initialState`.
module.exports.createStore = (initialState) ->
reducers = require './reducers'
reducer = combineReducers reducers
createStoreWithMiddleware = applyMiddleware(thunk)(createStore)
return createStoreWithMiddleware reducer, initialState