Created
February 23, 2012 01:46
-
-
Save sethladd/1889118 to your computer and use it in GitHub Desktop.
Dart IndexedDB sample
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
<html> | |
<head> | |
<title>indexeddb</title> | |
</head> | |
<body> | |
<h1>indexeddb</h1> | |
<button id="add">Add Item</button> | |
<ul id="items"></ul> | |
<script type="text/javascript" src="indexeddb.dart.js"></script> | |
<!--<script type="application/dart" src="indexeddb.dart"></script>--> | |
<!--<script src="http://dart.googlecode.com/svn/branches/bleeding_edge/dart/client/dart.js"></script>--> | |
</body> | |
</html> |
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
#import('dart:dom', prefix:'dom'); | |
#import('dart:html'); | |
String VERSION = "1"; | |
String TODOS_STORE = 'todos'; | |
initDb(db) { | |
if (VERSION != db.version) { | |
dom.IDBVersionChangeRequest versionChange = db.setVersion(VERSION); | |
versionChange.addEventListener('success', (e) { | |
print("Set version"); | |
db.createObjectStore(TODOS_STORE); | |
displayItems(db); | |
}); | |
versionChange.addEventListener('error', (e) { | |
print("Could not set version: $e"); | |
}); | |
} else { | |
print("DB is at version ${db.version}"); | |
displayItems(db); | |
} | |
} | |
displayItems(dom.IDBDatabase db) { | |
dom.IDBTransaction txn = db.transaction(TODOS_STORE, dom.IDBTransaction.READ_ONLY); | |
dom.IDBObjectStore objectStore = txn.objectStore(TODOS_STORE); | |
dom.IDBRequest cursorRequest = objectStore.openCursor(); | |
cursorRequest.addEventListener("success", (e) { | |
var cursor = e.target.result; | |
if (cursor != null) { | |
renderItem(cursor.value); | |
cursor.continueFunction(); | |
} | |
}); | |
cursorRequest.addEventListener('error', (e) { | |
print("Could not open cursor: $e"); | |
}); | |
} | |
renderItem(value) { | |
var ul = document.query('#items'); | |
var li = new Element.tag("li"); | |
li.text = value; | |
ul.elements.add(li); | |
} | |
addItem(db) { | |
var msg = "Stuff ${new Date.now().value}"; | |
dom.IDBTransaction txn = db.transaction(TODOS_STORE, dom.IDBTransaction.READ_WRITE); | |
dom.IDBObjectStore objectStore = txn.objectStore(TODOS_STORE); | |
dom.IDBRequest addRequest = objectStore.put(msg, new Date.now().value); | |
addRequest.addEventListener("success", (e) { | |
renderItem(msg); | |
}); | |
addRequest.addEventListener("error", (e) => print("Could not add: $e")); | |
} | |
registerButtons(db) { | |
var add = document.query("#add"); | |
add.on.click.add((e) { | |
addItem(db); | |
}); | |
} | |
// Chrome only for now, see bug http://code.google.com/p/chromium/issues/detail?id=108223 | |
void main() { | |
dom.IDBRequest request = dom.window.webkitIndexedDB.open('todo'); | |
request.addEventListener('success', (e) { | |
print("Opened DB"); | |
dom.IDBDatabase db = e.target.result; | |
// the following is Chrome only for now :( | |
initDb(db); | |
registerButtons(db); | |
}); | |
request.addEventListener('error', (e) { | |
print('Could not open db: $e'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will fail on the second reload of the page. See bug http://code.google.com/p/dart/issues/detail?id=1814