Skip to content

Instantly share code, notes, and snippets.

@rofrol
Last active January 29, 2016 14:30
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 rofrol/c11c3926742b0ce65b87 to your computer and use it in GitHub Desktop.
Save rofrol/c11c3926742b0ce65b87 to your computer and use it in GitHub Desktop.
cycle.js example
function main({DOM}) {
const h = CycleDOM.h;
return {
DOM:
// click, click, click -> [2.. 4.. 4..]
DOM.select('button').events('click').
map(evt => parseInt(evt.target.innerText)).
filter(val => val % 2 === 0).
// [2.. 4.. 4..] -> [2].. [2, 4].. [2, 4, 4]..
scan((state, n) => (
state.concat(n)
), []).
// [2].. [2, 4].. [2, 4, 4].. -> virtual-doms
startWith([]).
map(state => h('div', [
h('div', [
h('button', ['1']),
h('button', ['2']),
h('button', ['3']),
h('button', ['4'])
]),
// render `state` as a <li>s
h('ul', state.map(n =>
h('li', [`You clicked ${n}`]))
)
]))
};
}
Cycle.run(main, {
DOM: CycleDOM.makeDOMDriver('#app')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment