Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. var yo = require('yo-yo')
  2. var sendAction = require('send-action')
  3.  
  4. /*
  5. * Create send function.
  6. */
  7. var send = sendAction({
  8. onaction: onaction,
  9. onchange: onchange,
  10. state: { value: 'ok' }
  11. })
  12.  
  13. /*
  14. * Set up the action handler to modify state based on the actions triggered
  15. */
  16. function onaction (action, state) {
  17. if (action.type === 'example') {
  18. return { value: action.value }
  19. }
  20. }
  21.  
  22. /*
  23. * Subscribe to changes to the store for rendering & logging
  24. */
  25. function onchange (action, state, oldState) {
  26. yo.update(document.getElementById('app'), render(state))
  27. }
  28.  
  29. /*
  30. * Render the html of the app with yo-yo
  31. */
  32. function render (state) {
  33. return div(state)
  34. }
  35.  
  36. document.body.appendChild(render(send.state()))
  37.  
  38. /*
  39. * Send an action to the store
  40. */
  41. send({ type: 'example', value: 'cool' })
  42.  
  43. /*
  44. * Alternate `send` syntax
  45. */
  46.  
  47. setTimeout( function () {
  48. send('example', { value: 'awesome' })
  49. }, 2000)
  50.  
  51. /*
  52. * Create a component to render
  53. */
  54. function div (state) {
  55. return yo`<div id="app">${state.value}</div>`
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement