Last active
February 2, 2018 22:00
-
-
Save callil/eb8d1540753259c46be77407a60e877b to your computer and use it in GitHub Desktop.
Choo simple store & view change
This file contains 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
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') |
This file contains 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
// 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> | |
` | |
} |
This file contains 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
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') | |
} | |
} |
This file contains 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
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