1 (function(window){2 3 var _db = null;4 var _name = 'casper';5 var _store = 'StaticMap';6 var _version = 16;7 8 // Allow other browsers:9 window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;10 11 window.staticMap = {12 'open':function(callback) {13 var request = indexedDB.open(_name, _version);14 15 request.onupgradeneeded = function(event) {16 _db = event.target.result;17 18 // Remove all structures19 var i;20 for (i=0; i<_db.objectStoreNames.length; i++){21 _db.deleteObjectStore(_db.objectStoreNames[i]);22 }23 24 // Create new structures25 var objectStore = _db.createObjectStore(_store, { keyPath: "key" });26 27 callback(false);28 }29 30 request.onsuccess = function(event) {31 _db = event.target.result;32 callback(true);33 }34 },35 'get':function(key, callback) {36 var request = _db.transaction([_store], "readwrite").objectStore(_store).get(key);37 request.onsuccess = function(event) {38 if (typeof event.target.result == 'undefined') {39 callback(null);40 } else {41 callback(event.target.result.value);42 }43 };44 },45 'set':function(key, value, callback) {46 var transaction = _db.transaction([_store], "readwrite");47 48 transaction.onerror = function(event) {49 // Don't forget to handle errors!50 callback(false);51 };52 53 var objectStore = transaction.objectStore(_store);54 55 var request = objectStore.add({'key':key,'value':value});56 57 request.onsuccess = function(event) {58 callback(true);59 };60 },61 'unset':function(key, callback) {62 var request = _db.transaction([_store], "readwrite").objectStore(_store).delete(key);63 request.onsuccess = function(event) {64 callback(false);65 };66 }67 }68 69 }(this))
Este ShareCode tiene versiones:
- Web Storage (v0.1) This version is using... (16/11/2012)
- Web Storage (v0.0) This version starts f... (24/04/2013)
- Web Storage (v0.0) This version starts f... (24/04/2013)
- Web Storage (v0.0) This version starts f... (24/04/2013)
- Web Storage (v0.0) This version starts f... (24/04/2013)
- Web Storage (v0.0) This version starts f... (24/04/2013)
Enlace
El enlace para compartir es: