|
1 | 1 | (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 | + |
2 | 27 | $.extend({
|
3 | 28 | /**
|
4 | 29 | * The IndexedDB object used to open databases
|
5 | 30 | * @param {Object} dbName - name of the database
|
6 | 31 | * @param {Object} config - version, onupgradeneeded, onversionchange, schema
|
7 | 32 | */
|
8 | 33 | "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 |
| - |
13 | 34 | if (config) {
|
14 | 35 | // Parse the config argument
|
15 | 36 | if (typeof config === "number") config = {
|
|
439 | 460 | },
|
440 | 461 | "transaction": function(storeNames, mode){
|
441 | 462 | !$.isArray(storeNames) && (storeNames = [storeNames]);
|
442 |
| - mode = mode || IDBTransaction.READ_WRITE; |
| 463 | + mode = getDefaultTransaction(mode); |
443 | 464 | return $.Deferred(function(dfd){
|
444 | 465 | dbPromise.then(function(db, e){
|
445 | 466 | try {
|
|
492 | 513 | dfd.rejectWith(trans, [e, e]);
|
493 | 514 | }
|
494 | 515 | }
|
495 |
| - me.transaction(storeName, typeof mode === "number" ? mode : IDBTransaction.READ_WRITE).then(function(){ |
| 516 | + me.transaction(storeName, getDefaultTransaction(mode)).then(function(){ |
496 | 517 | //console.log"Transaction completed");
|
497 | 518 | // Nothing to do when transaction is complete
|
498 | 519 | }, function(err, e){
|
|
513 | 534 | db.close();
|
514 | 535 | }
|
515 | 536 | };
|
516 |
| - me.transaction(storeName, typeof mode === "number" ? mode : IDBTransaction.READ_WRITE).then(function(){ |
| 537 | + me.transaction(storeName, getDefaultTransaction(mode)).then(function(){ |
517 | 538 | //console.log"Transaction completed when trying to create object store");
|
518 | 539 | // Nothing much to do
|
519 | 540 | }, function(err, e){
|
|
594 | 615 | }
|
595 | 616 | });
|
596 | 617 |
|
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; |
600 | 621 | })(jQuery);
|
0 commit comments