Created
January 31, 2016 20:57
-
-
Save rofrol/a4731c3d4522824c9028 to your computer and use it in GitHub Desktop.
cycle.js embed single in medium.com
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 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') | |
}); |
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
<!doctype html> | |
<!-- https://medium.com/@fkrautwald/we-are-not-writing-much-code-5404fb7d39e --> | |
<div id="app"></div> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.0.7/rx.all.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/cyclejs-core/6.0.0/cycle.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/cyclejs-dom/9.0.2/cycle-dom.min.js"></script> | |
<script src="app.js"></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment