Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement