-
Notifications
You must be signed in to change notification settings - Fork 477
/
todo.js
62 lines (53 loc) · 1.03 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
class Todo {
constructor() {
this.id = 1
this.todos = [{
id: 0,
title: '测试',
done: false,
}, {
id: 1,
title: '学习 Westore',
done: true,
}]
this.onChanges = []
}
subscribe = function (onChange) {
this.onChanges.push(onChange)
}
inform() {
this.onChanges.forEach(function (cb) { cb() })
}
addTodo(title) {
this.todos.unshift({
id: ++this.id,
title: title,
done: false
})
this.inform()
}
clearDone() {
this.todos = this.todos.filter(function (todo) {
return !todo.done
})
this.inform()
}
destroy(id) {
this.todos = this.todos.filter(function (candidate) {
return candidate.id !== id
})
this.inform()
}
toggleAll(checked) {
this.todos.map(function (todo) {
todo.done = checked
})
this.inform()
}
toggle(id) {
const todoToToggle = this.todos.find(todo => todo.id === id)
todoToToggle.done = !todoToToggle.done
this.inform()
}
}
module.exports = Todo