Skip to content

Commit eed4ec9

Browse files
author
Parashuram
committed
Corrected transaction modes.
If the user specifies a valid string or numeric value, the mode passes through transparently, otherwise read_write mode is applied. Note: It is best to use browser constants for modes as they are supported by earlier versions also.
1 parent 02e905f commit eed4ec9

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

jquery.indexeddb.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
11
(function($, undefined){
2+
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
3+
var IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange;
4+
var IDBCursor = window.IDBCursor || window.webkitIDBCursor;
5+
6+
/**
7+
* Best to use the constant IDBTransaction since older version support numeric types while the latest spec
8+
* supports strings
9+
*/
10+
var IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;
11+
12+
function getDefaultTransaction(mode){
13+
var result = null;
14+
switch (mode) {
15+
case 0:
16+
case 1:
17+
case "readwrite":
18+
case "readonly":
19+
result = mode;
20+
break;
21+
default:
22+
result = IDBTransaction.READ_WRITE;
23+
}
24+
return result;
25+
}
26+
227
$.extend({
328
/**
429
* The IndexedDB object used to open databases
530
* @param {Object} dbName - name of the database
631
* @param {Object} config - version, onupgradeneeded, onversionchange, schema
732
*/
833
"indexedDB": function(dbName, config){
9-
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
10-
var IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange;
11-
var IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;
12-
1334
if (config) {
1435
// Parse the config argument
1536
if (typeof config === "number") config = {
@@ -439,7 +460,7 @@
439460
},
440461
"transaction": function(storeNames, mode){
441462
!$.isArray(storeNames) && (storeNames = [storeNames]);
442-
mode = mode || IDBTransaction.READ_WRITE;
463+
mode = getDefaultTransaction(mode);
443464
return $.Deferred(function(dfd){
444465
dbPromise.then(function(db, e){
445466
try {
@@ -492,7 +513,7 @@
492513
dfd.rejectWith(trans, [e, e]);
493514
}
494515
}
495-
me.transaction(storeName, typeof mode === "number" ? mode : IDBTransaction.READ_WRITE).then(function(){
516+
me.transaction(storeName, getDefaultTransaction(mode)).then(function(){
496517
//console.log"Transaction completed");
497518
// Nothing to do when transaction is complete
498519
}, function(err, e){
@@ -513,7 +534,7 @@
513534
db.close();
514535
}
515536
};
516-
me.transaction(storeName, typeof mode === "number" ? mode : IDBTransaction.READ_WRITE).then(function(){
537+
me.transaction(storeName, getDefaultTransaction(mode)).then(function(){
517538
//console.log"Transaction completed when trying to create object store");
518539
// Nothing much to do
519540
}, function(err, e){
@@ -594,7 +615,7 @@
594615
}
595616
});
596617

597-
$.indexedDB.IDBCursor = window.IDBCursor || window.webkitIDBCursor;
598-
$.indexedDB.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;
599-
618+
$.indexedDB.IDBCursor = IDBCursor;
619+
$.indexedDB.IDBTransaction = IDBTransaction;
620+
$.idb = $.indexedDB;
600621
})(jQuery);

0 commit comments

Comments
 (0)