Skip to content

Instantly share code, notes, and snippets.

@rosstimson
Created June 4, 2011 21:00
Show Gist options
  • Save rosstimson/1008352 to your computer and use it in GitHub Desktop.
Save rosstimson/1008352 to your computer and use it in GitHub Desktop.
SproutCore 2.0 ToDo App in CoffeeScript
# SproutCore 2.0 ToDo App in CoffeeScript
# BSD3, Jinjing Wang 2011
#
# Ross Timson: Added in a few CoffeeScript syntactic niceties
Todos = SC.Application.create()
Todos.Todo = SC.Object.extend
title: null
isDone: false
Todos.todosController = SC.ArrayProxy.create
# Initialize the array controller with an empty array.
content: []
# Creates a new todo with the passed title, then adds
# it to the array.
createTodo: (title) ->
todo = Todos.Todo.create title: title
@pushObject todo
remaining: (->
@filterProperty('isDone', false).get('length')
).property '@each.isDone'
clearCompletedTodos: ->
@filterProperty('isDone', true).forEach(@removeObject, @)
allAreDone: ((key, value) ->
if value isnt undefined
@setEach('isDone', value)
value
else
@get('length') and @everyProperty('isDone', true)
).property '@each.isDone'
Todos.StatsView = SC.View.extend
remainingBinding: 'Todos.todosController.remaining'
remainingString: (->
remaining = @get 'remaining'
remaining + (if remaining is 1 then " item" else " items")
).property 'remaining'
Todos.CreateTodoView = SC.TextField.extend
insertNewline: ->
value = @get 'value'
if value
Todos.todosController.createTodo value
@set 'value', ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment