Choo: Where is this send comes from ?

Created on 27 Jun 2016  路  7Comments  路  Source: choojs/choo

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

Most helpful comment

@crapthings note that onSubmit is a function within a function, thus it has access to the parent function's scope.

All 7 comments

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 can I assume this has been resolved?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

corderophi678 picture corderophi678  路  5Comments

awesomektvn picture awesomektvn  路  5Comments

depoulo picture depoulo  路  3Comments

knownasilya picture knownasilya  路  3Comments

pekeler picture pekeler  路  6Comments