非Firefox浏览器中的IndexedDB getAll
IndexedDB getAll in non-Firefox browsers
我知道IDBObjectStore.getAll
不是IndexedDB标准的一部分,它可能永远不会。但它是在FireFox中实现的,如果你必须从数据库中检索大量对象,它会让你的代码更漂亮。
是否可以制作某种polyfill或其他允许getAll
在其他支持IndexedDB的浏览器中工作的东西?getAll
的实际功能很简单,但我不知道如何在非Firefox浏览器中复制IndexedDB的精确语法的情况下处理它的异步特性。
我为一个垫片制作了一个GitHub repo,以在其他浏览器中支持getAll,这在Chrome中似乎足够好。代码重复如下以供后人使用:
(function () {
"use strict";
var Event, getAll, IDBIndex, IDBObjectStore, IDBRequest;
IDBObjectStore = window.IDBObjectStore || window.webkitIDBObjectStore || window.mozIDBObjectStore || window.msIDBObjectStore;
IDBIndex = window.IDBIndex || window.webkitIDBIndex || window.mozIDBIndex || window.msIDBIndex;
if (typeof IDBObjectStore.prototype.getAll !== "undefined" && typeof IDBIndex.prototype.getAll !== "undefined") {
return;
}
// https://github.com/axemclion/IndexedDBShim/blob/gh-pages/src/IDBRequest.js
IDBRequest = function () {
this.onsuccess = null;
this.readyState = "pending";
};
// https://github.com/axemclion/IndexedDBShim/blob/gh-pages/src/Event.js
Event = function (type, debug) {
return {
"type": type,
debug: debug,
bubbles: false,
cancelable: false,
eventPhase: 0,
timeStamp: new Date()
};
};
getAll = function (key) {
var request, result;
key = typeof key !== "undefined" ? key : null;
request = new IDBRequest();
result = [];
// this is either an IDBObjectStore or an IDBIndex, depending on the context.
this.openCursor(key).onsuccess = function (event) {
var cursor, e, target;
cursor = event.target.result;
if (cursor) {
result.push(cursor.value);
cursor.continue();
} else {
if (typeof request.onsuccess === "function") {
e = new Event("success");
e.target = {
readyState: "done",
result: result
};
request.onsuccess(e);
}
}
};
return request;
};
if (typeof IDBObjectStore.prototype.getAll === "undefined") {
IDBObjectStore.prototype.getAll = getAll;
}
if (typeof IDBIndex.prototype.getAll === "undefined") {
IDBIndex.prototype.getAll = getAll;
}
}());
相关文章:
- 加载存储在IndexedDB中的HTML页面
- 将IndexedDB中的数据拉入数组,并通过ReactJS输出
- IndexedDB对象键:大小是否重要
- 在IndexedDB中保留空间
- IndexedDB同步api+web工作者-what'这就是重点
- 为什么我看到“;类型错误:游标未定义“;当使用indexedDB时
- 如何检查在IndexedDB中是否找不到键
- Indexeddb:如何限制返回的对象数
- 非Firefox浏览器中的IndexedDB getAll
- 在indexedDB中调用onupgradewneed方法的时间和原因
- "由于页面导航“,尚未完成的IndexedDB事务已中止”;
- indexedDB创建数据库并添加内容执行失败'交易'在'IDBDatabase'
- 索引IndexedDB中对象中的数组值
- 如何在火狐浏览器上增加 IndexedDB 的配额
- AngularJS 在 IndexedDB 调用后返回方法的对象
- 错误“由于与数据库无关的原因,操作失败..“,在 Firefox 中使用 indexedDB 时
- Chrome devtools extension for IndexedDB
- Chrome 扩展 cookies.getAll 不起作用
- JayData 提供程序无法为 sqLite 和 indexedDB 加载
- 有关 Javascript IndexedDB 输入信息的问题