Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save avanslaars/5c3910962067f1e813a7144b11099e32 to your computer and use it in GitHub Desktop.
Save avanslaars/5c3910962067f1e813a7144b11099e32 to your computer and use it in GitHub Desktop.
Simple form that shows field value and clears field on button click. Tracks input to text and uses its value at the time of the button click and to update the state that the view is driven from.
//http://www.webpackbin.com/N1KplM91b
import Motorcycle from '@motorcycle/core'
import {div, label, input, hr, h1, makeDOMDriver} from '@motorcycle/dom';
import most from 'most'
function main(sources) {
const input$ = sources.DOM.select('.field').events('input')
const nameChange$ = input$
.map(ev => ({displayName:'', inputVal:ev.target.value}))
const click$ = sources.DOM.select('.btn').events('click')
const save$ = input$.sampleWith(click$)
.map(ev => ({displayName:ev.target.value, inputVal:''}))
const state$ = nameChange$.merge(save$).startWith({displayName:''})
const sinks = {
DOM: state$
.map(function(obj){
return div([
label('Name:'),
input('.field', {attrs: {type: 'text'}, props:{
value: obj.inputVal}}),
input('.btn', {attrs:{type:'submit',value:'Save'}}),
hr(),
h1('Hello ' + obj.displayName)
])
})
};
return sinks;
}
Motorcycle.run(main, { DOM: makeDOMDriver('#app') });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment