-
Notifications
You must be signed in to change notification settings - Fork 41
/
ydn.db-1.0-tests.ts
82 lines (64 loc) · 1.54 KB
/
ydn.db-1.0-tests.ts
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
/// <reference path="ydn.db-1.0.ts" />
var schema = {
stores: [{
name: 'todo',
keyPath: "timeStamp"
}]
};
/**
* Create and initialize the database. Depending on platform, this will
* create IndexedDB or WebSql or even localStorage storage mechanism.
* @type {ydn.db.Storage}
*/
var db = new ydn.db.Storage('todo_2', schema);
var deleteTodo = function(id: any) {
db.remove('todo', id).fail(function(e) {
console.error(e);
});
getAllTodoItems();
};
var getAllTodoItems = function() {
var todos = document.getElementById("todoItems");
todos.innerHTML = "";
var df = db.values('todo');
df.done(function(items) {
var n = items.length;
for (var i = 0; i < n; i++) {
renderTodo(items[i]);
}
});
df.fail(function(e) {
console.error(e);
})
};
var renderTodo = function(row: any) {
var todos = document.getElementById("todoItems");
var li = document.createElement("li");
var a = document.createElement("a");
var t = document.createTextNode(row.text);
a.addEventListener("click", function() {
deleteTodo(row.timeStamp);
}, false);
a.textContent = " [Delete]";
li.appendChild(t);
li.appendChild(a);
todos.appendChild(li)
};
var addTodo = function() {
var todo = <HTMLInputElement>document.getElementById("todo");
var data = {
"text": todo.value,
"timeStamp": new Date().getTime()
};
db.put('todo', data).fail(function(e) {
console.error(e);
});
todo.value = "";
getAllTodoItems();
};
function init() {
getAllTodoItems();
}
db.onReady(function() {
init();
});