choo: add server rendering

This commit is contained in:
Yoshua Wuyts
2016-05-22 02:06:46 +09:00
parent ffca94d59a
commit 4084775c8c
6 changed files with 150 additions and 5 deletions
+18 -1
View File
@@ -14,15 +14,31 @@ module.exports = choo
function choo (opts) {
opts = opts || {}
const name = opts.name || 'choo'
const _models = []
var _router = null
var _models = [ appInit(opts) ]
start.toString = toString
start.router = router
start.model = model
start.start = start
return start
// render the application to a string
function toString (route, state) {
const initialState = {}
_models.forEach(function (model) {
if (model.state) apply(model.name, model.state, initialState)
})
const tree = _router(route, xtend(initialState, state), function () {
throw new Error('send() cannot be called on the server')
})
return tree.toString()
}
// start the application
// null -> DOMNode
function start () {
@@ -30,6 +46,7 @@ function choo (opts) {
const reducers = {}
const effects = {}
_models.push(appInit(opts))
_models.forEach(function (model) {
if (model.state) apply(model.name, model.state, initialState)
if (model.reducers) apply(model.name, model.reducers, reducers)