Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. class Actor {
  2. constructor({state, actions, view}) {
  3. this.state = state
  4.  
  5. for (let [key, value] of Object.entries(actions)) {
  6. if (typeof value == "function") {
  7. actions[key] = value.bind(this, state)
  8. }
  9. }
  10.  
  11. this.actions = actions
  12. this.view = view
  13. }
  14. }
  15.  
  16. const view = new Actor({
  17. state: {
  18. count: 0
  19. },
  20. actions: {
  21. increment: function(state) {
  22. state.count++
  23. },
  24. decriment: function(state) {
  25. state.count--
  26. },
  27. addX: function(state, x) {
  28. state.count += x
  29. }
  30. },
  31. view: function() {
  32. const {state, actions} = this
  33.  
  34. return m('div', [
  35. m('h1', state.count),
  36. m('button', {
  37. onclick: actions.increment
  38. }, '+1'),
  39. m('button', {
  40. onclick: actions.decriment
  41. }, '-1'),
  42. m('button', {
  43. onclick: actions.addX.bind(null, 5)
  44. }, '+5')
  45. ])
  46. },
  47. });
  48.  
  49. m.mount(document.body, view)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement