Merge pull request #111 from yoshuawuyts/change-view-arguments

Change view arguments
This commit is contained in:
Yoshua Wuyts
2016-07-02 22:59:56 +02:00
committed by GitHub
15 changed files with 85 additions and 50 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
const html = require('../../../html') const html = require('../../../html')
module.exports = function (params, state, send) { module.exports = function (state, prev, send) {
const error = state.app.errors[0] const error = state.app.errors[0]
const title = state.api.title const title = state.api.title
return html` return html`
+2 -1
View File
@@ -1,7 +1,8 @@
const dateformat = require('dateformat') const dateformat = require('dateformat')
const html = require('../../../html') const html = require('../../../html')
module.exports = function (params, state, send) { module.exports = function (state, prev, send) {
const params = state.params
const mailbox = params.mailbox const mailbox = params.mailbox
const messages = state[mailbox].messages const messages = state[mailbox].messages
return html` return html`
+2 -1
View File
@@ -1,6 +1,7 @@
const html = require('../../../html') const html = require('../../../html')
module.exports = function (params, state, send) { module.exports = function (state, prev, send) {
const params = state.params
const mailbox = params.mailbox const mailbox = params.mailbox
const message = params.message const message = params.message
+1 -1
View File
@@ -1,6 +1,6 @@
const html = require('../../../html') const html = require('../../../html')
module.exports = function (params, state, send) { module.exports = function (state, prev, send) {
return html` return html`
<section> <section>
<p>Select a mailbox</p> <p>Select a mailbox</p>
+2 -1
View File
@@ -2,7 +2,8 @@ const dateformat = require('dateformat')
const html = require('../../../html') const html = require('../../../html')
module.exports = function () { module.exports = function () {
return function (params, state, send) { return function (state, prev, send) {
const params = state.params
const mailbox = params.mailbox const mailbox = params.mailbox
const message = params.message const message = params.message
const messages = state[mailbox].messages const messages = state[mailbox].messages
+1 -1
View File
@@ -2,7 +2,7 @@ const html = require('../../../html')
const mailboxes = [ 'inbox', 'spam', 'sent' ] const mailboxes = [ 'inbox', 'spam', 'sent' ]
module.exports = function (params, state, send) { module.exports = function (state, prev, send) {
return html` return html`
<aside class="fl mt4 w-20 db"> <aside class="fl mt4 w-20 db">
<ul> <ul>
+2 -2
View File
@@ -1,8 +1,8 @@
const pathname = require('pathname-match') const pathname = require('pathname-match')
const html = require('../../../html') const html = require('../../../html')
module.exports = function (params, state, send) { module.exports = function (state, prev, send) {
const location = state.app.location const location = state.location.pathname
return html` return html`
<span class="fl mt4 w-100 f4 b"> <span class="fl mt4 w-100 f4 b">
URL: ${pathname(location) || '/'} URL: ${pathname(location) || '/'}
+5 -5
View File
@@ -5,14 +5,14 @@ const pathname = require('../elements/pathname')
const email = require('../elements/email') const email = require('../elements/email')
const nav = require('../elements/nav') const nav = require('../elements/nav')
module.exports = function (params, state, send) { module.exports = function (state, prev, send) {
return html` return html`
<main class="mw5 mw7-ns center cf"> <main class="mw5 mw7-ns center cf">
${pathname(params, state, send)} ${pathname(state, prev, send)}
${nav(params, state, send)} ${nav(state, prev, send)}
<section class="fl mt4 w-80 db"> <section class="fl mt4 w-80 db">
${emailList(params, state, send)} ${emailList(state, prev, send)}
${email(params, state, send)} ${email(state, prev, send)}
</section> </section>
</main> </main>
` `
+4 -4
View File
@@ -4,12 +4,12 @@ const empty = require('../elements/empty-mailbox')
const pathname = require('../elements/pathname') const pathname = require('../elements/pathname')
const nav = require('../elements/nav') const nav = require('../elements/nav')
module.exports = function (params, state, send) { module.exports = function (state, prev, send) {
return html` return html`
<main class="mw5 mw7-ns center cf"> <main class="mw5 mw7-ns center cf">
${pathname(params, state, send)} ${pathname(state, prev, send)}
${nav(params, state, send)} ${nav(state, prev, send)}
${empty(params, state, send)} ${empty(state, prev, send)}
</main> </main>
` `
} }
+4 -4
View File
@@ -4,13 +4,13 @@ const emailList = require('../elements/email-list')
const pathname = require('../elements/pathname') const pathname = require('../elements/pathname')
const nav = require('../elements/nav') const nav = require('../elements/nav')
module.exports = function (params, state, send) { module.exports = function (state, prev, send) {
return html` return html`
<main class="mw5 mw7-ns center cf"> <main class="mw5 mw7-ns center cf">
${pathname(params, state, send)} ${pathname(state, prev, send)}
${nav(params, state, send)} ${nav(state, prev, send)}
<section class="fl mt4 w-80 db"> <section class="fl mt4 w-80 db">
${emailList(params, state, send)} ${emailList(state, prev, send)}
</section> </section>
</main> </main>
` `
+1 -1
View File
@@ -1,7 +1,7 @@
const assert = require('assert') const assert = require('assert')
const html = require('../../../html') const html = require('../../../html')
module.exports = function (params, state, send) { module.exports = function (state, prev, send) {
const serverMessage = state.message.server const serverMessage = state.message.server
const clientMessage = state.message.client const clientMessage = state.message.client
+1 -1
View File
@@ -15,7 +15,7 @@ app.model({
} }
}) })
const mainView = (params, state, send) => { const mainView = (state, prev, send) => {
return html` return html`
<main class="app"> <main class="app">
<h1>${state.input.title}</h1> <h1>${state.input.title}</h1>
+53 -21
View File
@@ -16,9 +16,11 @@ module.exports = choo
function choo (opts) { function choo (opts) {
opts = opts || {} opts = opts || {}
const _store = barracks(xtend(opts, { onState: render })) const _store = start._store = barracks(xtend(opts, { onState: render }))
var _router = start._router = null
var _defaultRoute = null
var _rootNode = null var _rootNode = null
var _router = null var _routes = null
start.toString = toString start.toString = toString
start.router = router start.router = router
@@ -34,11 +36,17 @@ function choo (opts) {
assert.equal(typeof route, 'string', 'choo.app.toString: route must be a string') assert.equal(typeof route, 'string', 'choo.app.toString: route must be a string')
assert.equal(typeof serverState, 'object', 'choo.app.toString: serverState must be an object') assert.equal(typeof serverState, 'object', 'choo.app.toString: serverState must be an object')
_store.start({ noSubscriptions: true, noReducers: true, noEffects: true }) _store.start({ noSubscriptions: true, noReducers: true, noEffects: true })
const state = _store.state({ state: serverState }) const state = _store.state({ state: serverState })
const tree = _router(route, state, function () { const router = createRouter(_defaultRoute, _routes, createSend)
assert.fail('choo: send() cannot be called from Node') const tree = router(route, state)
})
return tree.toString() return tree.toString()
function createSend () {
return function send () {
assert.fail('choo: send() cannot be called from Node')
}
}
} }
// start the application // start the application
@@ -52,18 +60,18 @@ function choo (opts) {
_store.model(appInit(startOpts)) _store.model(appInit(startOpts))
const createSend = _store.start(startOpts) const createSend = _store.start(startOpts)
const send = createSend('view', true) _router = start._router = createRouter(_defaultRoute, _routes, createSend)
const state = _store.state() const state = _store.state()
if (!selector) { if (!selector) {
const tree = _router(state.app.location, state, send) const tree = _router(state.location.pathname, state)
_rootNode = tree _rootNode = tree
return tree return tree
} else { } else {
document.addEventListener('DOMContentLoaded', function (event) { document.addEventListener('DOMContentLoaded', function (event) {
const oldTree = document.querySelector(selector) const oldTree = document.querySelector(selector)
assert.ok(oldTree, 'could not query selector: ' + selector) assert.ok(oldTree, 'could not query selector: ' + selector)
const newTree = _router(state.app.location, state, send) const newTree = _router(state.location.pathname, state)
_rootNode = yo.update(oldTree, newTree) _rootNode = yo.update(oldTree, newTree)
}) })
} }
@@ -75,17 +83,15 @@ function choo (opts) {
if (opts.onState) opts.onState(action, state, prev, name, createSend) if (opts.onState) opts.onState(action, state, prev, name, createSend)
if (state === prev) return if (state === prev) return
// note(yw): only here till sheet-router supports custom constructors const newTree = _router(state.location.pathname, state, prev)
const send = createSend('view', true)
const newTree = _router(state.app.location, state, send, prev)
_rootNode = yo.update(_rootNode, newTree) _rootNode = yo.update(_rootNode, newTree)
} }
// register all routes on the router // register all routes on the router
// (str?, [fn|[fn]]) -> obj // (str?, [fn|[fn]]) -> obj
function router (defaultRoute, cb) { function router (defaultRoute, routes) {
_router = sheetRouter(defaultRoute, cb) _defaultRoute = defaultRoute
return _router _routes = routes
} }
// create a new model // create a new model
@@ -93,16 +99,42 @@ function choo (opts) {
function model (model) { function model (model) {
_store.model(model) _store.model(model)
} }
// create a new router with a custom `createRoute()` function
// (str?, obj, fn?) -> null
function createRouter (defaultRoute, routes, createSend) {
var prev = {}
return sheetRouter(defaultRoute, routes, createRoute)
function createRoute (routeFn) {
return function (route, inline, child) {
if (typeof inline === 'function') {
inline = wrap(inline, route)
}
return routeFn(route, inline, child)
}
function wrap (child, route) {
const send = createSend(route, true)
return function chooWrap (params, state) {
const nwPrev = prev
const nwState = prev = xtend(state, { params: params })
if (!opts.noFreeze) Object.freeze(nwState)
return child(nwState, nwPrev, send)
}
}
}
}
} }
// initial application state model // initial application state model
// obj -> obj // obj -> obj
function appInit (opts) { function appInit (opts) {
const loc = document.location const loc = document.location
const state = { location: (opts.hash) ? hashMatch(loc.hash) : loc.href } const state = { pathname: (opts.hash) ? hashMatch(loc.hash) : loc.href }
const reducers = { const reducers = {
location: function setLocation (action, state) { setLocation: function setLocation (action, state) {
return { location: action.location.replace(/#.*/, '') } return { pathname: action.location.replace(/#.*/, '') }
} }
} }
// if hash routing explicitly enabled, subscribe to it // if hash routing explicitly enabled, subscribe to it
@@ -114,12 +146,12 @@ function appInit (opts) {
}) })
}, 'handleHash', subs) }, 'handleHash', subs)
} else { } else {
if (opts.history !== false) pushLocationSub(history, 'setLocation', subs) if (opts.history !== false) pushLocationSub(history, 'handleHistory', subs)
if (opts.href !== false) pushLocationSub(href, 'handleHref', subs) if (opts.href !== false) pushLocationSub(href, 'handleHref', subs)
} }
return { return {
namespace: 'app', namespace: 'location',
subscriptions: subs, subscriptions: subs,
reducers: reducers, reducers: reducers,
state: state state: state
@@ -130,8 +162,8 @@ function appInit (opts) {
// (fn, obj) -> null // (fn, obj) -> null
function pushLocationSub (cb, key, model) { function pushLocationSub (cb, key, model) {
model[key] = function (send, done) { model[key] = function (send, done) {
cb(function navigate (href) { cb(function navigate (pathname) {
send('app:location', { location: href }, done) send('location:setLocation', { location: pathname }, done)
}) })
} }
} }
+1 -1
View File
@@ -30,7 +30,7 @@
"sheet-router": "^3.1.0", "sheet-router": "^3.1.0",
"xhr": "^2.2.0", "xhr": "^2.2.0",
"xtend": "^4.0.1", "xtend": "^4.0.1",
"yo-yo": "^1.2.0" "yo-yo": "^1.2.2"
}, },
"devDependencies": { "devDependencies": {
"bankai": "^2.0.2", "bankai": "^2.0.2",
+5 -5
View File
@@ -21,7 +21,7 @@ tape('should render on the server', function (t) {
const app = choo() const app = choo()
app.router((route) => [ app.router((route) => [
route('/', function (params, state) { route('/', function (state, prev, send) {
return view`<h1>meow meow ${state.message}</h1>` return view`<h1>meow meow ${state.message}</h1>`
}) })
]) ])
@@ -37,7 +37,7 @@ tape('should render on the server', function (t) {
const app = choo() const app = choo()
app.model({ state: { bin: 'baz', beep: 'boop' } }) app.model({ state: { bin: 'baz', beep: 'boop' } })
app.router((route) => [ app.router((route) => [
route('/', function (params, state) { route('/', function (state, prev, send) {
return view`<h1>${state.foo} ${state.bin} ${state.beep}</h1>` return view`<h1>${state.foo} ${state.bin} ${state.beep}</h1>`
}) })
]) ])
@@ -57,7 +57,7 @@ tape('should render on the server', function (t) {
state: { bin: 'baz', beep: 'boop' } state: { bin: 'baz', beep: 'boop' }
}) })
app.router((route) => [ app.router((route) => [
route('/', function (params, state) { route('/', function (state, prev, send) {
return view` return view`
<h1>${state.hello.foo} ${state.hello.bin} ${state.hello.beep}</h1> <h1>${state.hello.foo} ${state.hello.bin} ${state.hello.beep}</h1>
` `
@@ -80,7 +80,7 @@ tape('should render on the server', function (t) {
const app = choo() const app = choo()
app.router((route) => [ app.router((route) => [
route('/', function (params, state, send) { route('/', function (state, prev, send) {
send('hey!') send('hey!')
}) })
]) ])
@@ -93,7 +93,7 @@ tape('should render on the server', function (t) {
const app = choo() const app = choo()
app.router((route) => [ app.router((route) => [
route('/', function (params, state, send) { route('/', function (state, prev, send) {
send('hey!') send('hey!')
}) })
]) ])