T.js example 2 - a simple TODO list

https://github.com/gcao/T.js/

HTML

<script src="http://gcao.github.io/T.js/public/javascripts/t.js"></script>
T.js example 2

This example shows a simple TODO list.

CoffeeScript

$('body').html T.render ->
  [ "div#container"
    style: 
      width: "300px"
      padding: "3px"
      border: "1px solid grey"

    [ "div.header"
      style:
        padding: "3px"
        background: "#fbb"
        "font-weight": "bold"
      "My TODO List"
    ]
    [ "div.contents"
      style: width: "100%"
    ]
    [ "input"
      type: "text"
      style: width: "98%"
      onkeyup: "handleInput(event, this)"
      placeholder: "Type content and hit Enter"
    ]
  ]

    
T.def 'rowTemplate', (item, index) ->
  [ "div.content"
    style: padding: "3px"

    T.escape(item)
    "&nbsp;&nbsp;"
    [ "a"
      href: "javascript:void(deleteTodo(#{index}))"
      "X"
    ]
  ]

T.def 'rowsTemplate', (rows) ->
  T('rowTemplate', item, i) for item, i in rows

contents = ["Do this", "Do that"]

updateContents = ->
  $('.contents').html(T('rowsTemplate').render(contents))

updateContents()

# Event handlers  
this.handleInput = (event, target) ->
  if event.which is 13
    event.preventDefault()
    contents.push $(target).val()
    $(target).val('')
    updateContents()

this.deleteTodo = (i) ->
  contents.splice(i, 1)
  updateContents()