-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvoodoo.html
112 lines (94 loc) · 3.25 KB
/
voodoo.html
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/styles.css">
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="js/knockout-2.0.0.js"></script>
</head>
<body>
<div id="todoapp">
<div class="title">
<h1>Todo</h1>
</div>
<form data-bind="submit: addTask" id="create-todo">
<input class="new-todo" data-bind="value: newTaskText" placeholder="What needs to be done?" />
</form>
<div class="todos">
<ul data-bind="foreach: tasks, visible: tasks().length > 0" id="todo-list">
<li>
<div class="todo" data-bind="css: { editing: isEditing }, event: { dblclick: startEdit }">
<div class="display" data-bind="css: { done: isDone }">
<input type="checkbox" class="check" data-bind="checked: isDone" />
<div class="todo-text" data-bind="text: title"></div>
<a href="#" class="todo-destroy" data-bind="click: $parent.removeTask">×</a>
</div>
<div class="edit">
<form data-bind="submit: updateTask">
<input data-bind="value: title" />
</form>
</div>
</div>
</li>
</ul>
</div>
You have <b data-bind="text: incompleteTasks().length"> </b> incomplete task(s)
<span data-bind="visible: incompleteTasks().length == 0"> - its beer time!</span>
<a data-bind="visible: completeTasks().length > 0, click: removeCompleted " href="#" id="clear-completed">Clear Completed Tasks</a>
</div>
<div id="credits">
<p>Dbl-click to edit tasks.</p>
</div>
<script>
/* data goes here */
var data = [
{id: 1, title: "Drink beer", isDone: false},
{id: 2, title: "Eat food", isDone: false},
{id: 3, title: "Snowboard", isDone: false}
];
function Task(data) {
this.title = ko.observable(data.title);
this.isDone = ko.observable(data.isDone);
this.isEditing = ko.observable(data.isEditing);
this.startEdit = function (event) {
console.log("editing");
this.isEditing(true);
}
this.updateTask = function (task) {
this.isEditing(false);
}
}
function TaskListViewModel() {
// Data
var self = this;
self.tasks = ko.observableArray([]);
self.newTaskText = ko.observable();
self.incompleteTasks = ko.computed(function() {
return ko.utils.arrayFilter(self.tasks(),
function(task) {
return !task.isDone() && !task._destroy;
});
});
self.completeTasks = ko.computed(function(){
return ko.utils.arrayFilter(self.tasks(),
function(task) {
return task.isDone() && !task._destroy;
});
});
// Operations
self.addTask = function() {
self.tasks.push(new Task({ title: this.newTaskText(), isEditing: false }));
self.newTaskText("");
};
self.removeTask = function(task) { self.tasks.destroy(task) };
self.removeCompleted = function(){
self.tasks.destroyAll(self.completeTasks());
};
/* Load the data */
var mappedTasks = $.map(data, function(item){
return new Task(item);
});
self.tasks(mappedTasks);
}
ko.applyBindings(new TaskListViewModel());
</script>
</body>
</html>