IndexedDB
IndexedDB example
by Quique Fdez Guerra
JavaScript
(function(g) {
g.App = {};
g.App.localDB = {
/*options: {
name: 'mydb',
version: 1,
onsucces: function(){},
onupgradeneeded: function(){},
onfailure: function(){},
}*/
open: function(options) {
console.log('Creating DB');
var self = this;
if(this.version != options.version) {
this.name = options.name;
this.version = options.version
this.stores = {};
var request = indexedDB.open(this.name, this.version);
}else {
console.log('This version is used');
}
request.onupgradeneeded = function(e) {
console.log('DB Created');
self.db = e.target.result;
if(options.onupgradeneeded) {
options.onupgradeneeded();
}
};
//request.onupgradeneeded = options.onupgradeneeded || function() {};
request.onfailure = options.onfailure || function() {};
},
/*options: {
storeName: 'myStore',
keyPath: 'timeStamp',
onsuccess: function(){},
onerror: function(){},
}*/
addStore: function(options, callback) {
console.log('Creating Store');
this.stores[options.storeName] = this.db.createObjectStore(
options.storeName,
{keyPath: options.keyPath}
);
console.log('Store created');
if(options.callback) { options.callback(); }
},
/*options: {
store: 'myStore',
put: {},
callback: function() {}
}*/
addItem: function(options) {
debugger
var trans = this.db.transaction([options.store], "readwrite");
var store = trans.objectStore(options.store);
var request = store.put(options.put);
request.onsuccess = options.onsuccess() || function() {};
request.onerror = options.onerror() || function() {};
}
};
var test = function() {console.log(App);};
App.localDB.open({
name: 'mydb',
version: 1,
onupgradeneeded: function(e) {
App.localDB.addStore({
storeName: 'myStore',
keyPath:...