Skip to content

Commit 02e905f

Browse files
author
Parashuram
committed
Added index.get and index.getKey methods
- Fallback for Chrome since the methods are not present
1 parent 81c530a commit 02e905f

File tree

2 files changed

+72
-5
lines changed

2 files changed

+72
-5
lines changed

jquery.indexeddb.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,18 @@
244244
}, callback);
245245
},
246246
"get": function(key){
247-
console.log(idbIndex);
248-
return wrap.request(
249-
idbIndex.get(key)
250-
);
247+
if (typeof idbIndex.get === "function") {
248+
return wrap.request(idbIndex.get(key));
249+
} else {
250+
return idbIndex.openCursor(wrap.range(key));
251+
}
252+
},
253+
"getKey": function(key){
254+
if (typeof idbIndex.getKey === "function") {
255+
return wrap.request(idbIndex.getKey(key));
256+
} else {
257+
return idbIndex.openKeyCursor(wrap.range(key));
258+
}
251259
}
252260
};
253261
}
@@ -571,8 +579,11 @@
571579
"eachKey": function(callback, range, direction){
572580
return indexOp("eachKey", indexName, [callback, range, direction]);
573581
},
574-
"get": function(key) {
582+
"get": function(key){
575583
return indexOp("get", indexName, [key]);
584+
},
585+
"getKey": function(key){
586+
return indexOp("getKey", indexName, [key]);
576587
}
577588
};
578589
}

test/index.html

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,62 @@
538538
});
539539
});
540540

541+
queuedAsyncTest("Getting index values with key", function(){
542+
var key = sample.integer();
543+
var value = sample.obj();
544+
_("Trying to add" + key + " and " + JSON.stringify(value));
545+
$.indexedDB(DB.NAME).objectStore(DB.OBJECT_STORE_1).add(value, key).then(function(res, e){
546+
ok(true, "Added " + res + " to the database");
547+
_("Added" + res + "to the database ");
548+
start();
549+
stop();
550+
$.indexedDB(DB.NAME).objectStore(DB.OBJECT_STORE_1).index("Int").get(value.Int).then(function(res, e){
551+
deepEqual(res, value);
552+
_("Got return value as" + res);
553+
start();
554+
nextTest();
555+
}, function(err, e){
556+
ok(false, "Index Iteration NOT completed");
557+
_("Index Iteration NOT completed");
558+
start();
559+
nextTest();
560+
});
561+
}, function(){
562+
ok(false, "Could not add value to the database");
563+
_("Could not add to database");
564+
start();
565+
nextTest();
566+
});
567+
});
568+
569+
queuedAsyncTest("Getting index-key values with key", function(){
570+
var key = sample.integer();
571+
var value = sample.obj();
572+
_("Trying to add" + key + " and " + JSON.stringify(value));
573+
$.indexedDB(DB.NAME).objectStore(DB.OBJECT_STORE_1).add(value, key).then(function(res, e){
574+
ok(true, "Added " + res + " to the database");
575+
_("Added" + res + "to the database ");
576+
start();
577+
stop();
578+
$.indexedDB(DB.NAME).objectStore(DB.OBJECT_STORE_1).index("Int").getKey(value.Int).then(function(res, e){
579+
_("Got return value as" + res);
580+
equal(key, res);
581+
start();
582+
nextTest();
583+
}, function(err, e){
584+
ok(false, "Index Iteration NOT completed");
585+
_("Index Iteration NOT completed");
586+
start();
587+
nextTest();
588+
});
589+
}, function(){
590+
ok(false, "Could not add value to the database");
591+
_("Could not add to database");
592+
start();
593+
nextTest();
594+
});
595+
});
596+
541597
queuedAsyncTest("Index iteration with keyCursor", function(){
542598
var key = sample.integer();
543599
var value = sample.obj();

0 commit comments

Comments
 (0)