Skip to content

Instantly share code, notes, and snippets.

@indatawetrust
Created March 17, 2019 12:09
Show Gist options
  • Save indatawetrust/5c279d8612556fdc10d6f13d2caa8b90 to your computer and use it in GitHub Desktop.
Save indatawetrust/5c279d8612556fdc10d6f13d2caa8b90 to your computer and use it in GitHub Desktop.
class Actor {
constructor({state, actions, view}) {
this.state = state
for (let [key, value] of Object.entries(actions)) {
if (typeof value == "function") {
actions[key] = value.bind(this, state)
}
}
this.actions = actions
this.view = view
}
}
const view = new Actor({
state: {
count: 0
},
actions: {
increment: function(state) {
state.count++
},
decriment: function(state) {
state.count--
},
addX: function(state, x) {
state.count += x
}
},
view: function() {
const {state, actions} = this
return m('div', [
m('h1', state.count),
m('button', {
onclick: actions.increment
}, '+1'),
m('button', {
onclick: actions.decriment
}, '-1'),
m('button', {
onclick: actions.addX.bind(null, 5)
}, '+5')
])
},
});
m.mount(document.body, view)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment