Skip to content

Commit

Permalink
1. Fix the old setVersion code - It's great to delete old code :)
Browse files Browse the repository at this point in the history
2. Change the readme file to md.
  • Loading branch information
greenido committed Jul 4, 2013
1 parent 5d069a7 commit ada5502
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 39 deletions.
File renamed without changes.
55 changes: 16 additions & 39 deletions jqm_indexedDB.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@

<script>
var dbName = "jqm-todo";
var dbVersion = 1;
var dbVersion = 4;
var todoDB = {};
var indexedDB = window.indexedDB || window.webkitIndexedDB ||
window.mozIndexedDB;

if ('webkitIndexedDB' in window) {
window.IDBTransaction = window.webkitIDBTransaction;
// window.IDBTransaction = window.webkitIDBTransaction;
window.IDBKeyRange = window.webkitIDBKeyRange;
}

todoDB.indexedDB = {};
todoDB.indexedDB.db = null;


$(document).bind('pageinit', function() {
console.log("-- lets start the party --");
Expand All @@ -49,52 +48,30 @@
request.onsuccess = function(e) {
console.log ("success our DB: " + dbName + " is open and ready for work");
todoDB.indexedDB.db = e.target.result;
var db = todoDB.indexedDB.db;
if (db.setVersion) {
console.log("in old setVersion: "+ db.setVersion);
if (db.version != dbVersion) {
var req = db.setVersion(dbVersion);
req.onsuccess = function () {
if(db.objectStoreNames.contains("todo")) {
db.deleteObjectStore("todo");
}

var store = db.createObjectStore("todo", {keyPath: "timeStamp"});
var trans = req.result;
trans.oncomplete = function(e) {
console.log("== oncomplete transaction ==");
todoDB.indexedDB.getAllTodoItems();
}
};
}
else {
todoDB.indexedDB.getAllTodoItems();
}
}
else {
todoDB.indexedDB.getAllTodoItems();
}
todoDB.indexedDB.getAllTodoItems();
}

request.onupgradeneeded = function(e) {
todoDB.indexedDB.db = e.target.result;
var db = todoDB.indexedDB.db;
console.log ("Going to upgrade our DB from version: "+ db.version + " to " + dbVersion);
if (db.version != dbVersion) {
if(db.objectStoreNames.contains("todo")) {
db.deleteObjectStore("todo");
}
console.log ("Going to upgrade our DB from version: "+ e.oldVersion + " to " + e.newVersion);

try {
if (db.objectStoreNames && db.objectStoreNames.contains("todo")) {
db.deleteObjectStore("todo");
}
}
catch (err) {
console.log("got err in objectStoreNames:" + err);
}
var store = db.createObjectStore("todo",
{keyPath: "timeStamp"});
console.log("-- onupgradeneeded store:"+ JSON.stringify(store));
}
}

request.onfailure = function(e) {
console.error("could not open our DB! Err:"+e);
}


request.onerror = function(e) {
console.error("Well... How should I put it? We have some issues with our DB! Err:"+e);
Expand All @@ -103,7 +80,7 @@

todoDB.indexedDB.addTodo = function(todoText) {
var db = todoDB.indexedDB.db;
var trans = db.transaction(['todo'], "readwrite");
var trans = todoDB.indexedDB.db.transaction("todo", "readwrite");
var store = trans.objectStore("todo");

var data = {
Expand All @@ -124,7 +101,7 @@

todoDB.indexedDB.deleteTodo = function(id) {
var db = todoDB.indexedDB.db;
var trans = db.transaction(["todo"], "readwrite");
var trans = db.transaction("todo", "readwrite");
var store = trans.objectStore("todo");

var request = store.delete(id);
Expand All @@ -143,7 +120,7 @@
todos.innerHTML = "";

var db = todoDB.indexedDB.db;
var trans = db.transaction(["todo"], "readwrite");
var trans = db.transaction("todo", "readonly");
var store = trans.objectStore("todo");

// Get everything in the store;
Expand Down Expand Up @@ -202,7 +179,7 @@
request.onsuccess = function(event) {
// Enumerate the entire object store.
var db = todoDB.indexedDB.db;
var trans = db.transaction(["todo"], IDBTransaction.READ_ONLY);
var trans = db.transaction("todo", "readonly");
var request = trans.objectStore("todo").openCursor();
var ul = document.createElement("ul");
request.onsuccess = function(event) {
Expand Down

0 comments on commit ada5502

Please sign in to comment.