forked from KitchenTableCoders/mvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.js
154 lines (129 loc) · 3.37 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
if(typeof window == "undefined") {
var Underscore = require("underscore");
}
;(function(global, j, _) {
var _id = 0;
function guid() {
var guid = _id;
_id++;
return guid;
}
var JST = {},
items = [{checked: false, title: "Get coffee", id: guid()},
{checked: false, title: "Get bagels", id: guid()},
{checked: false, title: "Get tea", id: guid()},
{checked: false, title: "Get sugar", id: guid()}];
var _listeners = [];
function addEvent(name, f) {
if(_listeners[name] == null) _listeners[name] = [];
_listeners[name].push(f);
}
function fireEvent(name) {
var args = Array.prototype.slice.call(arguments, 1);
_(_listeners[name]).each(function(f) {
f.apply(null, args);
});
}
function addItem(x) {
x.checked = x.checked || false;
x.id = guid();
items.push(x);
fireEvent("changed");
}
function updateItem(id, x) {
items[id] = x;
fireEvent("changed");
}
function removeItem(id) {
items.splice(id,1);
fireEvent("changed");
}
function findItem(id) {
return _(items).find(function(item) {
return item.id === parseInt(id);
});
}
function markDone(id) {
var item = findItem(id);
item.checked = true;
fireEvent("changed");
fireEvent("todoDone", id, item.title);
}
function markNotDone(id) {
var item = findItem(id);
item.checked = false;
fireEvent("changed");
fireEvent("todoNotDone", id, item.title);
}
function notDone() {
return _(items).filter(function(item) {
return !item.checked;
});
}
function done() {
return _(items).filter(function(item) {
return item.checked;
});
}
function renderItems() {
j("#todo").html(JST["todos"]({items:notDone()}));
j("#done").html(JST["todos"]({items:done()}));
fireEvent("rendered");
}
function initJST() {
_(j(".jst")).each(function(jst) {
jst = j(jst);
JST[jst.attr("name")] = _.template(jst.text());
});
}
function addEvents() {
j(document)
.on("click", "#todo input[type=checkbox]", function(e) {
var li = j(e.target).parent("li");
markDone(li.data("todo-id"));
})
.on("click", "#done input[type=checkbox]", function(e) {
var li = j(e.target).parent("li");
markNotDone(li.data("todo-id"));
})
}
function init() {
initJST();
addEvents();
// initial render
renderItems();
// if data changes, re-render
addEvent("changed", renderItems);
addEvent("rendered", function() {
console.log("rendered!");
});
addEvent("todoDone", function(id, title) {
console.log("todo done event:", id, title);
});
addEvent("todoNotDone", function(id, title) {
console.log("todo notDone event:", id, title);
});
console.log("init");
}
if(typeof window != "undefined") {
j(document).ready(init);
}
// for debugging & testing
var toExport = {
JST: JST,
items: items,
addItem: addItem,
removeItem: removeItem,
renderItems: renderItems,
markDone: markDone,
markNotDone: markNotDone
};
if(typeof window != "undefined") {
// For the browser
global.Todo = {};
_.extend(global.Todo, toExport);
} else {
// For node.js
exports.Todo = toExport;
}
})(this, (typeof jQuery != "undefined" ? jQuery : null), Underscore);