const document = require('global/document')
const choo = require('choo')
const http = require('choo/http')
const app = choo()
function view (params, state, send) {
return choo.view`
<form onsubmit=${onSubmit}>
<fieldset>
<label>username</label>
<input type="text" name="username" autofocus>
</fieldset>
<fieldset>
<label>password</label>
<input type="password" name="password">
</fieldset>
<input type="submit" value="Submit">
</form>
`
function onSubmit (event) {
send('login', { data: new FormData(event.target) }) // where is the send comes from ?
event.preventDefault()
}
}
app.model({
effects: {
login: (action, state, send) => {
http.post('/login', { body: action.data }, (err, res, body) => {
send('authorize', { payload: body })
})
}
}
})
app.router((route) => [
route('/', view)
])
app.start()
edit by @yoshuawuyts: updated for syntax highlighting
send() is a function that is passed in by choo in order to allow calling methods on the model. It's a function that allows decoupling of the callers and the callee, ensuring a unidrectional data flow. Currently we're using send-action under the hood, but that might change in a future patch. Does this answer your question? Cheers!
i think this send is lost context that lead 'undefined' in this example.
send('login', { data: new FormData(event.target) }) // where is the send comes from ?
it comes from:
function view (params, state, send) {
which in turn is passed there by the router:
app.router((route) => [
route('/', view)
])
It should def not be undefined; that means something's going wrong :/
@crapthings note that onSubmit is a function within a function, thus it has access to the parent function's scope.
i've copy & paste that code then i've got undefined
@crapthings should work: http://requirebin.com/?gist=0a545442880961d7a42270e0758d2af2
@crapthings can I assume this has been resolved?
Most helpful comment
@crapthings note that
onSubmitis a function within a function, thus it has access to the parent function's scope.