Search code examples
coffeescriptlivescript

Convert CoffeeScript code to LiveScript?


I have this valid CoffeeScript and wish to convert it to LiveScript. Can someone explain why it fails to convert? Also to give a converted one?

TodoCtrl = (scope) ->
  scope.addTodo = ->
    scope.todos.push
      text: scope.todoText
      done: false
    scope.todoText = ''

You can use this to compile CoffeeScript.

http://coffeescript.org/

You can use this to compile LiveScript.

http://gkz.github.com/LiveScript/


Solution

  • You are calling the function scope.todos.push against an implicit block starting with an implicit object. You must use do in LiveScript, as it does't do this special case (just think of do as parentheses around the block). See https://github.com/gkz/LiveScript/issues/50 for reasons.

    The code you want:

    TodoCtrl = (scope) ->
      scope.addTodo = ->
        scope.todos.push do
          text: scope.todoText
          done: false
        scope.todoText = ''
    

    which is equivalent to (ie do is just parentheses)

    TodoCtrl = (scope) ->
      scope.addTodo = ->
        scope.todos.push(
          text: scope.todoText
          done: false
        )
        scope.todoText = ''
    

    Glad to see you using LiveScript!