-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathtodo.js
78 lines (74 loc) · 1.63 KB
/
todo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import {Component, Template, bootstrap, Foreach} from 'angular2/angular2';
import {bind} from 'angular2/di';
import {TodoStore} from 'services/TodoStore';
@Component({
selector: 'todo-app',
componentServices: [
TodoStore
]
})
@Template({
url: 'todo.html',
directives: [Foreach]
})
class TodoApp {
todoStore: TodoStore;
todoEdit: any;
todos: Array;
constructor(store: TodoStore) {
this.todoStore = store;
this.todoEdit = null;
this.todos = store.list;
}
enterTodo($event, newTodo) {
if($event.which === 13) { // ENTER_KEY
this.addTodo(newTodo.value);
newTodo.value = '';
}
}
editTodo($event, todo) {
this.todoEdit = todo;
}
doneEditing($event, todo) {
var which = $event.which;
var target = $event.target;
if(which === 13) {
todo.title = target.value;
this.todoStore.save(todo);
this.todoEdit = null;
} else if (which === 27) {
this.todoEdit = null;
target.value = todo.title;
}
}
addTodo(newTitle) {
this.todoStore.add({
title: newTitle,
completed: false
});
}
completeMe(todo) {
todo.completed = !todo.completed;
this.todoStore.save(todo);
}
deleteMe(todo) {
this.todoStore.remove(todo);
}
toggleAll($event) {
var isComplete = $event.target.checked;
this.todoStore.list.forEach(function(todo) {
todo.completed = isComplete;
this.todoStore.save(todo);
}.bind(this));
}
clearCompleted() {
[].concat(this.todoStore.list).forEach((todo) => {
if(todo.completed) {
this.deleteMe(todo);
}
});
}
}
export function main() {
bootstrap(TodoApp);
}