Created
September 30, 2011 11:53
-
-
Save edelabar/1253537 to your computer and use it in GitHub Desktop.
Example JavaScript for working with HTML5 IndexedDB
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
var v = "1.0."+(new Date()).getTime(); | |
var iDB = window.webkitIndexedDB || window.mozIndexedDB; | |
var oReq = iDB.open("myDB"); | |
oReq.onsuccess = function(event) { | |
db = event.result || event.target.result; | |
var vReq = db.setVersion(v); | |
console.log(vReq); | |
vReq.onsuccess = function(e) { | |
console.log("Version set!",db); | |
cReq = db.createObjectStore("myStore", {keyPath:"myKey"}); | |
} | |
} |
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
var iDB = window.webkitIndexedDB || window.mozIndexedDB; | |
var oReq = iDB.open("myDB"); | |
oReq.onsuccess = function(e) { | |
db = event.target.result; | |
var trans = db.transaction(["myStore"],webkitIDBTransaction.READ_WRITE,0); | |
var store = trans.objectStore("myStore"); | |
var pReq = store.put({myKey:1,myValue:"Eric"}); | |
pReq.onsuccess = function(e) { | |
console.log("Successfully Inserted!",e); | |
} | |
} |
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
var iDB = window.webkitIndexedDB || window.mozIndexedDB; | |
var oReq = iDB.open("myDB"); | |
oReq.onsuccess = function(e) { | |
db = event.target.result; | |
var trans = db.transaction(["myStore"],webkitIDBTransaction.READ_WRITE,0); | |
var store = trans.objectStore("myStore"); | |
var keyRange = webkitIDBKeyRange.lowerBound(0); | |
var cReq = store.openCursor(keyRange); | |
cReq.onsuccess = function(e) { | |
var result = e.target.result; | |
if( !!result == false ) return; | |
console.log("Object: ",result.value); | |
result.continue(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment