Skip to content

Instantly share code, notes, and snippets.

@WreckedAvent
Last active October 6, 2019 23:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save WreckedAvent/9b3b1580e945c44d741e868abf73eef8 to your computer and use it in GitHub Desktop.
Save WreckedAvent/9b3b1580e945c44d741e868abf73eef8 to your computer and use it in GitHub Desktop.
basic data flow example
import * as m from 'mithril'
// this is a "presentation" component and knows nothing about the world
export const view = (_, { link }) => m('.nav', m('.breadcrumbs', [
m('a', { href: link })
]))
import * as m from 'mithril'
import * as x from './x'
import * as y from './y'
m.route(document.body, '/', {
'x': x,
'y': y
})
import * as m from 'mithril'
import * as breadcrumbs from './breadcrumbs'
// this is a "container" compnoent and is aware of at least some of our model
export const view = (_, { model, view }) => m('.container', [
m(breadcrumbs, { link: model.nav.link() }),
view
])
import * as m from 'mithril'
// we only export a factory so components cannot inexplicably require our state out of proper flow
export default () => ({
nav: {
link: m.prop()
},
main: {
something: m.prop()
}
})
import * as m from 'mithril'
import * as layout from './layout'
import * as something from './something'
export const controller = (model) => {
m.request('someserver')
.then(model.nav.link)
.then(m.redraw)
}
// this is a "container" component and is aware of at least some of our model
export const view = (_, model) => m(layout, {
// if we wanted to, we could turn the layout component into a presentation one by passing it
// whatever it needs instead of our model directly. Use your discretion when this would help or hinder
// maintainence in your app
// here, we could have dozens of independent views which all reference layout, and layout could be updated
// to require something new from the model at any point, requiring us to change all of those views
// so making it a "layout" component appears to be the better choice for keeping our app organized
// and maintainable
model,
view: m('.view', [
// "container" components can map our model however they need to to child components
m(something, { text: model.main.something() })
])
})
import * as m from 'mithril'
// this is a "presentation" component and knows nothing about the world
export const view = (_, { text }) => m('p', text)
import * as sharedView from './sharedView'
import modelFactory from './sharedModel'
// controller is understandable as our view's state
// we make it our model factory function so that we get
// a fresh state as the component is created
export const controller = modelFactory
// in a real app, we'd have more diversity between our views,
// but here we just call the shared view component directly in this toy example
export const view = (state) => m(sharedView, state)
import * as sharedView from './sharedView'
import modelFactory from './sharedModel'
// controller is understandable as our view's state
// we make it our model factory function so that we get
// a fresh state as the component is created
export const controller = modelFactory
// in a real app, we'd have more diversity between our views,
// but here we just call the shared view component directly in this toy example
export const view = (state) => m(sharedView, state)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment