关于 indexedDB 的奇怪之处之一是带有不在 objectstore 中的键的 objectStore.get() 请求会生成成功事件(event.target.results == undefined)
(请参阅:http ://www.w3.org/TR/IndexedDB/#object-存储get 函数)。
我需要捕获这种类型的“成功”并记录下来。现在,我在返回的事件中找不到 KEY。所以我正在做一个真正的黑客并将其存储在事务中。有什么建议吗?
var deferred = new jQuery.Deferred(); // this is what gets returned
var id = view.zoom + "-" + view.tile.column + "-" + view.tile.row;
trans = DB.transaction(["Images"], "readonly");
store = trans.objectStore("Images");
req = store.get(id);
trans._keyPathForGet = id; // hack
req.onsuccess = function (e) {
var blob = e.target.result;
if (blob == undefined) {
console.log("no blob of id: " + e.target.transaction._keyPathForGet);
deferred.resolve(null);
} else {
var imgURL = URL.createObjectURL(blob);
deferred.resolve(imgURL); // let leaflet know we're done, and give it the URL
URL.revokeObjectURL(imgURL); // not sure if we're revoking it too soon here
}