Skip to content

Instantly share code, notes, and snippets.

@AxelRHD
Last active January 24, 2021 00:37
Show Gist options
  • Save AxelRHD/27cd1feeb578d1c7708098c42d2ef19f to your computer and use it in GitHub Desktop.
Save AxelRHD/27cd1feeb578d1c7708098c42d2ef19f to your computer and use it in GitHub Desktop.
Cycle JS - counter M-V-I
<!DOCTYPE html>
<html>
<head>
<script src="https://npmcdn.com/[email protected]/dist/xstream.min.js"></script>
<script src="https://npmcdn.com/@cycle/[email protected]/dist/cycle-dom.min.js"></script>
<script src="https://npmcdn.com/@cycle/[email protected]/dist/cycle.min.js"></script>
<script src="https://npmcdn.com/@cycle/[email protected]/dist/cycle-http-driver.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div id="app"></div>
<script id="jsbin-javascript">
const xs = xstream.default;
const {div, button, p, makeDOMDriver} = CycleDOM;
function intent(domSources) {
return {
decrement$: domSources.select('.dec').events('click'),
increment$: domSources.select('.inc').events('click'),
}
}
function model(actions) {
return xs.merge(
actions.decrement$.mapTo(-1),
actions.increment$.mapTo(+1))
.fold((prev, x) => prev + x, 0)
}
function view(state$) {
return state$.map(count =>
div([
button('.dec', 'Decrement'),
button('.inc', 'Increment'),
p('Counter: ' + count)
]))
}
function main(sources) {
return {
DOM: view(model(intent(sources.DOM)))
};
}
Cycle.run(main, {
DOM: makeDOMDriver('#app')
});
</script>
<script id="jsbin-source-javascript" type="text/javascript">const xs = xstream.default;
const {div, button, p, makeDOMDriver} = CycleDOM;
function intent(domSources) {
return {
decrement$: domSources.select('.dec').events('click'),
increment$: domSources.select('.inc').events('click'),
}
}
function model(actions) {
return xs.merge(
actions.decrement$.mapTo(-1),
actions.increment$.mapTo(+1))
.fold((prev, x) => prev + x, 0)
}
function view(state$) {
return state$.map(count =>
div([
button('.dec', 'Decrement'),
button('.inc', 'Increment'),
p('Counter: ' + count)
]))
}
function main(sources) {
return {
DOM: view(model(intent(sources.DOM)))
};
}
Cycle.run(main, {
DOM: makeDOMDriver('#app')
});</script></body>
</html>
const xs = xstream.default;
const {div, button, p, makeDOMDriver} = CycleDOM;
function intent(domSources) {
return {
decrement$: domSources.select('.dec').events('click'),
increment$: domSources.select('.inc').events('click'),
}
}
function model(actions) {
return xs.merge(
actions.decrement$.mapTo(-1),
actions.increment$.mapTo(+1))
.fold((prev, x) => prev + x, 0)
}
function view(state$) {
return state$.map(count =>
div([
button('.dec', 'Decrement'),
button('.inc', 'Increment'),
p('Counter: ' + count)
]))
}
function main(sources) {
return {
DOM: view(model(intent(sources.DOM)))
};
}
Cycle.run(main, {
DOM: makeDOMDriver('#app')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment