Mithril Todo

by ramnathv

CoffeeScript

TodoList =
  view: (c, props) ->
    m 'ul',
      props.items.map (item, index) -> 
        m 'li', {key: index + item}, item
        
TodoApp =
  controller: ->
    text: m.prop("")
    items: m.prop([])
    add_item: (v) ->
      @items().push(v)
      @text("")
  view: (c) ->
    m 'div', [
      m 'h3', 'TODO'
      m 'input[type="text"]', {
          onchange: m.withAttr("value", c.add_item.bind(c))
          value: c.text()
        }
        m('button', "Add #{c.items().length + 1}")
      m.component(TodoList, {items: c.items()})
    ]
      
    
myTodos = m.component(TodoList, {items: ['T1', 'T2']})
m.mount(document.body, TodoApp)