Files
buuh/examples/async-counter/client.js
T

43 lines
1.1 KiB
JavaScript
Raw Normal View History

2016-07-09 13:55:49 -04:00
const choo = require('../../')
const html = require('../../html')
2016-07-09 14:03:09 -04:00
const plur = require('plur')
2016-07-09 13:55:49 -04:00
const app = choo()
app.model({
state: {
counter: 0
},
reducers: {
2016-12-11 19:35:29 +01:00
increment: (state, data) => ({ counter: state.counter + 1 }),
decrement: (state, data) => ({ counter: state.counter - 1 })
2016-07-09 13:55:49 -04:00
},
effects: {
2016-12-11 19:35:29 +01:00
incrementAsync: function (state, data, send, done) {
2016-07-09 13:55:49 -04:00
setTimeout(() => send('increment', done), 1000)
},
2016-12-11 19:35:29 +01:00
decrementAsync: function (state, data, send, done) {
2016-07-09 13:55:49 -04:00
setTimeout(() => send('decrement', done), 1000)
}
}
})
2016-12-11 19:35:29 +01:00
function mainView (state, prev, send) {
2016-07-23 16:23:24 +08:00
const count = state.counter
2016-07-09 14:03:09 -04:00
2016-07-09 13:55:49 -04:00
return html`
<main class="app">
<h1>Async counter</h1>
2016-07-09 14:03:09 -04:00
<p>Clicked ${count} ${plur('time', count)}!</p>
2016-07-09 13:55:49 -04:00
<button onclick=${() => send('increment')}>Increment</button>
<button onclick=${() => send('decrement')}>Decrement</button>
<button onclick=${() => send('incrementAsync')}>Increment async</button>
<button onclick=${() => send('decrementAsync')}>Decrement async</button>
</main>
`
}
2016-12-11 19:35:29 +01:00
app.router([ '/', mainView ])
2016-07-09 13:55:49 -04:00
const tree = app.start()
document.body.appendChild(tree)