-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix TypeScript definitions and add tests.
- Loading branch information
1 parent
70e45b5
commit 566edfe
Showing
2 changed files
with
176 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(); | ||
}); |
Oops, something went wrong.