Skip to content

Adding global listener for errors #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
});
```
Expand Down
19 changes: 14 additions & 5 deletions src/jquery.indexeddb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down