Created
January 5, 2019 18:14
-
-
Save abradley2/042e00747cf65d3ccee58cdd72c25113 to your computer and use it in GitHub Desktop.
Model-View-Intent
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
import xs from 'xstream' | |
import { run } from '@cycle/run' | |
import * as H from '@cycle/dom' | |
import create from 'immutability-helper' | |
const INPUT = 'INPUT' | |
function intent (DOM) { | |
const inputEvents = DOM.select('.field').events('input') | |
.map((e) => ({ | |
type: INPUT, | |
value: e.target.value | |
})) | |
return xs.merge( | |
inputEvents | |
) | |
} | |
const initialModel = { | |
message: 'hello there' | |
} | |
function model (state, action) { | |
switch (action.type) { | |
case INPUT: | |
return create(state, { | |
message: {$set: action.value} | |
}) | |
} | |
return state | |
} | |
function view (state) { | |
return H.div([ | |
H.div({}, state.message), | |
H.input({ | |
attrs: { | |
class: 'field', | |
value: state.message | |
} | |
}) | |
]) | |
} | |
function app (sources) { | |
return { | |
DOM: intent(sources.DOM) | |
.startWith({ type: 'INIT' }) | |
.fold(model, initialModel) | |
.map(view) | |
} | |
} | |
run(app, { DOM: H.makeDOMDriver('#app') }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment