Skip to content

Instantly share code, notes, and snippets.

@callil
Last active February 2, 2018 22:00
Show Gist options
  • Save callil/eb8d1540753259c46be77407a60e877b to your computer and use it in GitHub Desktop.
Save callil/eb8d1540753259c46be77407a60e877b to your computer and use it in GitHub Desktop.
Choo simple store & view change
var choo = require('choo')
var html = require('choo/html')
var app = choo()
var main = require('./views/main.js')
var store = require('./stores/store.js')
app.use(store)
app.route('/', main)
app.mount('body')
// import choo's template helper
const html = require('choo/html')
module.exports = Main
const question = require('./question.js')
function Main (state, emit) {
return html`
<body>
<div>
${question(state,emit)}
</div>
</body>
`
}
var html = require('choo/html')
module.exports = question
// export module
function question (state, emit) {
// create html template
return html`
<p onclick=${handleChangeQuestion}>${state.player.question}</p>
`
function handleChangeQuestion () {
emit('changeQuestion')
}
}
function store (state, emitter) {
state.player = {
iterator: 0,
question: "Who are you?"
}
function changeFooter () {
console.log('hi')
state.player.iterator = state.player.iterator+1
console.log(state.player.iterator)
switch (state.player.iterator) {
case 0:
state.player.question = "Where are you from?"
break;
case 1:
state.player.question = "What do you want?"
break;
default:
state.player.question = "Who are you?"
}
emitter.emit('render')
}
emitter.on('changeQuestion', changeFooter)
}
module.exports = store
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment