Merge pull request #116 from timwis/rename-action-param-to-data

Rename action param to data
This commit is contained in:
Yoshua Wuyts
2016-07-04 11:32:50 +02:00
committed by GitHub
7 changed files with 35 additions and 35 deletions
+15 -15
View File
@@ -128,7 +128,7 @@ can be called to modify it:
app.model({
state: { title: 'Set the title' },
reducers: {
update: (action, state) => ({ title: action.value })
update: (data, state) => ({ title: data.value })
}
})
```
@@ -173,7 +173,7 @@ const app = choo()
app.model({
state: { title: 'Set the title' },
reducers: {
update: (action, state) => ({ title: action.value })
update: (data, state) => ({ title: data.value })
}
})
@@ -203,9 +203,9 @@ sources of data. `effects` react to changes, perform an `action` and can then
post the results. `reducers` take data, modify it, and update the internal
`state`.
Communication of data is done using objects called `actions`. Each `action` has
any number of properties for data, and a unique `type` that can trigger
properties on the models.
Communication of data is done using objects called `actions`. Each `action`
consists of a unique `actionName` and an optional payload of `data`, which can
be any value.
When a `reducer` modifies `state`, the `router` is called, which in turn calls
`views`. `views` take `state` and return [DOM][dom] nodes which are then
@@ -256,7 +256,7 @@ app.model({
namespace: 'todos',
state: { todos: [] },
reducers: {
add: (action, state) => ({ todos: state.todos.concat(action.payload) })
add: (data, state) => ({ todos: state.todos.concat(data.payload) })
}
})
```
@@ -302,7 +302,7 @@ app.model({
(send) => setInterval(() => send('app:print', { payload: 'dog?' }), 1000)
],
effects: {
print: (action, state) => console.log(action.payload)
print: (data, state) => console.log(data.payload)
}
})
```
@@ -368,7 +368,7 @@ app.model({
}
})
function getJson (state, action, send) {
function getJson (state, data, send) {
http.get('/my-endpoint', { json: true }, function (err, res, body) {
if (err) return send('app:error', { payload: err.message })
if (res.statusCode !== 200 || !body) {
@@ -378,7 +378,7 @@ function getJson (state, action, send) {
})
}
function postJson (state, action, send) {
function postJson (state, data, send) {
const body = { foo: 'bar' }
http.post('/my-endpoint', { json: body }, function (err, res, body) {
if (err) return send('app:error', { payload: err.message })
@@ -389,7 +389,7 @@ function postJson (state, action, send) {
})
}
function httpDelete (state, action, send) {
function httpDelete (state, data, send) {
const body = { foo: 'bar' }
http.del('/my-endpoint', { json: body }, function (err, res, body) {
if (err) return send('app:error', { payload: err.message })
@@ -499,15 +499,15 @@ function view (params, state, send) {
`
function onSubmit (event) {
send('login', { data: new FormData(event.target) })
send('login', new FormData(event.target))
event.preventDefault()
}
}
app.model({
effects: {
login: (action, state, send) => {
http.post('/login', { body: action.data }, (err, res, body) => {
login: (data, state, send) => {
http.post('/login', { body: data }, (err, res, body) => {
send('authorize', { payload: body })
})
}
@@ -642,9 +642,9 @@ arguments:
in-namespace only.
- __state:__ object. Key value store of initial values
- __reducers:__ object. Syncronous functions that modify state. Each function
has a signature of `(action, state)`
has a signature of `(data, state)`
- __effects:__ object. Asyncronous functions that perform IO. Each function has
a signature of `(action, state, send)` where `send` is a reference to
a signature of `(data, state, send)` where `send` is a reference to
`app.send()`
### app.router(params, state, send)
+3 -3
View File
@@ -10,12 +10,12 @@ const app = choo({
const send = createSend('onError: ')
send('app:error', err)
},
onAction: function (action, state, name, caller, createSend) {
onAction: function (data, state, name, caller, createSend) {
console.groupCollapsed(`Action: ${caller} -> ${name}`)
console.log(action)
console.log(data)
console.groupEnd()
},
onState: function (action, state, prev, createSend) {
onState: function (data, state, prev, createSend) {
console.groupCollapsed('State')
console.log(prev)
console.log(state)
+4 -4
View File
@@ -6,13 +6,13 @@ module.exports = {
title: 'Button pushing machine 3000'
},
reducers: {
set: (action, state) => ({ 'title': action.data })
set: (data, state) => ({ 'title': data })
},
effects: {
good: function (action, state, send, done) {
good: function (data, state, send, done) {
request('/good', send, done)
},
bad: (action, state, send, done) => request('/bad', send, done)
bad: (data, state, send, done) => request('/bad', send, done)
}
}
@@ -26,6 +26,6 @@ function request (uri, send, done) {
return done(new Error(message))
}
if (!body) return done(new Error('fatal: no body received'))
send('api:set', { data: body.message || body.title }, done)
send('api:set', body.message || body.title, done)
})
}
+4 -4
View File
@@ -15,13 +15,13 @@ module.exports = {
triggerTime: null
},
reducers: {
setError: function (action, state) {
setError: function (data, state) {
return {
errors: state.errors.concat(action.message),
errorTimeDone: action.errorTimeDone
errors: state.errors.concat(data.message),
errorTimeDone: data.errorTimeDone
}
},
'delError': function (action, state) {
'delError': function (data, state) {
state.errors.shift()
return { errors: state.errors }
}
+3 -3
View File
@@ -35,13 +35,13 @@ function createModel () {
}
],
reducers: {
'print': (action, state) => {
return ({ msg: state.msg + ' ' + action.payload })
'print': (data, state) => {
return ({ msg: state.msg + ' ' + data.payload })
}
},
effects: {
close: () => stream.close(),
error: (action, state) => console.error(`error: ${action.payload}`)
error: (data, state) => console.error(`error: ${data.payload}`)
}
}
}
+2 -2
View File
@@ -8,10 +8,10 @@ app.model({
title: 'my demo app'
},
reducers: {
update: (action, state) => ({ title: action.payload })
update: (data, state) => ({ title: data.payload })
},
effects: {
update: (action, state, send) => (document.title = action.payload)
update: (data, state, send) => (document.title = data.payload)
}
})
+4 -4
View File
@@ -79,8 +79,8 @@ function choo (opts) {
// update the DOM after every state mutation
// (obj, obj, obj, str, fn) -> null
function render (action, state, prev, name, createSend) {
if (opts.onState) opts.onState(action, state, prev, name, createSend)
function render (data, state, prev, name, createSend) {
if (opts.onState) opts.onState(data, state, prev, name, createSend)
if (state === prev) return
const newTree = _router(state.location.pathname, state, prev)
@@ -133,8 +133,8 @@ function appInit (opts) {
const loc = document.location
const state = { pathname: (opts.hash) ? hashMatch(loc.hash) : loc.href }
const reducers = {
setLocation: function setLocation (action, state) {
return { pathname: action.location.replace(/#.*/, '') }
setLocation: function setLocation (data, state) {
return { pathname: data.location.replace(/#.*/, '') }
}
}
// if hash routing explicitly enabled, subscribe to it