-
-
Save indatawetrust/5c279d8612556fdc10d6f13d2caa8b90 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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