Created
June 4, 2011 21:00
-
-
Save rosstimson/1008352 to your computer and use it in GitHub Desktop.
SproutCore 2.0 ToDo App in CoffeeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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