Skip to content

Instantly share code, notes, and snippets.

@timwis
Created December 27, 2016 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timwis/51cba0526de46c49c2f35428cee52a0e to your computer and use it in GitHub Desktop.
Save timwis/51cba0526de46c49c2f35428cee52a0e to your computer and use it in GitHub Desktop.
const choo = require('choo')
const html = require('choo/html')
const widget = require('cache-element/widget')
const app = choo()
app.model({
state: {
rows: [ {a: 'a'}, {b: 'b'} ]
},
reducers: {
addRow: (state, data) => {
const newRows = [...state.rows, {c: 'c'}]
return { rows: newRows }
}
}
})
app.router(['/', View])
const tree = app.start()
document.body.appendChild(tree)
function View (state, prev, send) {
return html`
<main>
${state.rows.map(Row)}
<button onclick=${onClick}>Add row</button>
</main>
`
function onClick () {
send('addRow')
}
}
function Row (config) {
const vizInstance = Viz()
return html`
<div>
${vizInstance(config)}
</div>
`
}
function Viz () {
console.log('init viz')
return widget({
render: (config) => {
console.log('render', config)
return html`
<div>
<div onload=${() => console.log('onload')}>
<pre>${JSON.stringify(config)}</pre>
</div>
</div>
`
},
onupdate: (el, config) => {
console.log('onupdate')
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment