Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 72 lines (57 sloc) 1.909 kb
62ad4de @siuying updated to latest TodoMVC
authored
1 Tasks = new Meteor.Collection('tasks')
2 ENTER_KEY = 13
1848d77 @siuying init commit
authored
3
4 if Meteor.is_client
62ad4de @siuying updated to latest TodoMVC
authored
5 Template.todo.tasks = ->
6 Tasks.find {}, {sort: {completed: 1}}
1848d77 @siuying init commit
authored
7
62ad4de @siuying updated to latest TodoMVC
authored
8 Template.todo.hasTodo = ->
9 Tasks.find({}).count() > 0
1848d77 @siuying init commit
authored
10
62ad4de @siuying updated to latest TodoMVC
authored
11 Template.todo.incompleted = ->
12 Tasks.find({completed: false}).count()
1848d77 @siuying init commit
authored
13
62ad4de @siuying updated to latest TodoMVC
authored
14 Template.todo.incompleted_text = ->
15 count = Tasks.find({completed: false}).count()
16 if count == 1
17 ' item left'
18 else
19 ' items left'
1848d77 @siuying init commit
authored
20
62ad4de @siuying updated to latest TodoMVC
authored
21 Template.todo.completed = ->
22 Tasks.find({completed: true}).count()
1848d77 @siuying init commit
authored
23
62ad4de @siuying updated to latest TodoMVC
authored
24 Template.todo.events =
25 'keyup #new-todo' : (evt) ->
26 if evt.type == 'keyup' && evt.which == ENTER_KEY
27 textbox = $('#new-todo')
28 Tasks.insert {title: textbox.val(), completed: false}
29 textbox.val('')
30 textbox.focus()
31 return false
1848d77 @siuying init commit
authored
32
62ad4de @siuying updated to latest TodoMVC
authored
33 'click #clear-completed': ->
34 Tasks.remove {completed: true}
35 return false
1848d77 @siuying init commit
authored
36
62ad4de @siuying updated to latest TodoMVC
authored
37 'click #mark-all-checked': (evt) ->
38 Tasks.update {}, {$set: {completed: true}}, {multi: true}
39 $(evt.target).removeAttr('checked')
1848d77 @siuying init commit
authored
40
62ad4de @siuying updated to latest TodoMVC
authored
41 Template.item.events =
42 'click .toggle': (evt) ->
43 task = Tasks.findOne this._id
44 task.completed = $(evt.target).attr('checked') == 'checked'
45 Tasks.update {_id: this._id}, task
1848d77 @siuying init commit
authored
46
62ad4de @siuying updated to latest TodoMVC
authored
47 'click .destroy': (evt) ->
48 Tasks.remove {_id: this._id}
1848d77 @siuying init commit
authored
49
62ad4de @siuying updated to latest TodoMVC
authored
50 'dblclick label': (evt) ->
51 task = Tasks.findOne this._id
52 task.editing = true
53 edit = $(evt.target).parent().parent().find('.edit')
54 Tasks.update {_id: this._id}, task, (err) ->
55 edit.focus().select() unless err
29ffdd4 @siuying refactor - rearranging some code
authored
56
62ad4de @siuying updated to latest TodoMVC
authored
57 'blur input.edit': (evt) ->
58 task = Tasks.findOne this._id
59 task.editing = false
60 Tasks.update {_id: this._id}, task
e071dff @siuying error handling
authored
61
62ad4de @siuying updated to latest TodoMVC
authored
62 'keyup input.edit': (evt) ->
63 if evt.type == 'keyup' && evt.which == ENTER_KEY
64 task = Tasks.findOne this._id
65 task.editing = false
66 task.updated = new Date()
67 task.title = $(evt.target).val()
1848d77 @siuying init commit
authored
68
62ad4de @siuying updated to latest TodoMVC
authored
69 Tasks.update {_id: this._id}, task, (err) =>
70 alert('Sorry, an error prevent the changes to be saved') if err
71 return false
Something went wrong with that request. Please try again.