From a6848220c7860a790344062d62bf671b470e212a Mon Sep 17 00:00:00 2001 From: jehon Date: Wed, 30 Oct 2013 21:01:39 +0100 Subject: [PATCH] Adding global listener for errors This function will be called whenever a Deferred is ending in "fail". --- docs/README.md | 6 ++++++ src/jquery.indexeddb.js | 19 ++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/docs/README.md b/docs/README.md index 6e2d5ec..bd09e41 100644 --- a/docs/README.md +++ b/docs/README.md @@ -44,6 +44,12 @@ var dbOpenPromise = $.indexedDB("database_name", { var obj2 = transaction.createObjectStore(store2); obj1.createIndex("index"); } + }, + "onError" : function(error, event) { + // (Additional) Function called whenever you receive an error on any Deferred + // This function come in handy in the development phase, when you don't specify + // all the "fail" handler, and still get a lot of trouble. You better specify a function + // here when starting a new development. } }); ``` diff --git a/src/jquery.indexeddb.js b/src/jquery.indexeddb.js index 4486359..dce0ff2 100644 --- a/src/jquery.indexeddb.js +++ b/src/jquery.indexeddb.js @@ -55,9 +55,18 @@ } + function getDeferred(fn) { + return $.Deferred(function(dfd) { + if (config && typeof(config.onError) == "function") { + dfd.fail(config.onError); + } + fn(dfd); + }) + } + var wrap = { "request": function(req, args) { - return $.Deferred(function(dfd) { + return getDeferred(function(dfd) { try { var idbRequest = typeof req === "function" ? req(args) : req; idbRequest.onsuccess = function(e) { @@ -187,7 +196,7 @@ }, "cursor": function(idbCursor, callback) { - return $.Deferred(function(dfd) { + return getDeferred(function(dfd) { try { console.log("Cursor request created", idbCursor); var cursorReq = typeof idbCursor === "function" ? idbCursor() : idbCursor; @@ -336,7 +345,7 @@ "deleteDatabase": function() { // Kinda looks ugly coz DB is opened before it needs to be deleted. // Blame it on the API - return $.Deferred(function(dfd) { + return getDeferred(function(dfd) { dbPromise.then(function(db, e) { db.close(); wrap.request(function() { @@ -358,7 +367,7 @@ "transaction": function(storeNames, mode) { !$.isArray(storeNames) && (storeNames = [storeNames]); mode = getDefaultTransaction(mode); - return $.Deferred(function(dfd) { + return getDeferred(function(dfd) { dbPromise.then(function(db, e) { var idbTransaction; try { @@ -397,7 +406,7 @@ result = {}; function op(callback) { - return $.Deferred(function(dfd) { + return getDeferred(function(dfd) { function onTransactionProgress(trans, callback) { try { console.log("Finally, returning the object store", trans);