Mithril: Simple Patch for Components

by ramnathv

CoffeeScript

## https://gist.github.com/impinball/5e4d6e64aba2ffb7cd23
## Patches Mithril to accept components as well as strings
@m = ((o) ->
  Object.assign (->
    (if typeof arguments[0] == 'string' then o else o.component).apply undefined, arguments
  ), o
)(m)

m.bind = (x) ->
  value: x()
  oninput: m.withAttr("value", x)

Hello = 
  view: (ctrl, props) ->
    m "h2", "Hello #{props.name}"
    
Input = 
  view: (ctrl, props) ->
    m "input[type='text']", m.bind(props.name)
    
App = 
  controller: ->
    vm: 
      text: m.prop("")
  view: (ctrl, props) ->
    console.log ctrl
    m "div", [
      m Input, {name: ctrl.vm.text}
      m Hello, {name: ctrl.vm.text()}
    ]

m.mount(
  document.body,
  m(App)
)