Skip to content

Instantly share code, notes, and snippets.

@andersonleite
Created January 4, 2017 00:10
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 andersonleite/300b51a3abdd67733ec99e443261d087 to your computer and use it in GitHub Desktop.
Save andersonleite/300b51a3abdd67733ec99e443261d087 to your computer and use it in GitHub Desktop.
const {div, input, label, h2, makeDOMDriver} = CycleDOM;
function main(sources) {
const changeWeight$ = sources.DOM.select('.weight').events('input')
.map(ev => ev.target.value);
const changeHeight$ = sources.DOM.select('.height').events('input')
.map(ev => ev.target.value);
const state$ = Rx.Observable.combineLatest(
changeWeight$.startWith(70),
changeHeight$.startWith(170),
(weight, height) => {
const heightMeters = height * 0.01;
const bmi = Math.round(weight / (heightMeters * heightMeters));
return {bmi, weight, height};
}
)
return {
DOM: state$.map(state =>
div([
div([
label('Weigth: ' + state.weight + 'kg'),
input('.weight', {type: 'range', min: 40, max: 150, value:state.weight})
]),
div([
label('Heigth: ' + state.height + 'cm'),
input('.height', {type: 'range', min: 140, max: 220, value: state.height})
]),
h2('BMI is ' + state.bmi)
])
)
};
}
const drivers = {
DOM: makeDOMDriver('#app')
};
Cycle.run(main, drivers);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment