最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - IndexedDB getAll in non-Firefox browsers - Stack Overflow

programmeradmin1浏览0评论

I am aware that IDBObjectStore.getAll is not part of the IndexedDB standard and that it might never be. But it is implemented in FireFox, and it makes your code prettier if you do have to retrieve a lot of objects from the database.

Would it be possible to make some kind of polyfill or something to allow getAll to work in other browsers that support IndexedDB? The actual functionality of getAll is simple, but I don't know how to deal with the asynchronous nature of IndexedDB in the context of replicating its precise syntax in non-Firefox browsers.

I am aware that IDBObjectStore.getAll is not part of the IndexedDB standard and that it might never be. But it is implemented in FireFox, and it makes your code prettier if you do have to retrieve a lot of objects from the database.

Would it be possible to make some kind of polyfill or something to allow getAll to work in other browsers that support IndexedDB? The actual functionality of getAll is simple, but I don't know how to deal with the asynchronous nature of IndexedDB in the context of replicating its precise syntax in non-Firefox browsers.

Share Improve this question asked Aug 25, 2012 at 15:40 dumbmatterdumbmatter 9,7037 gold badges45 silver badges87 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 12

I made a GitHub repo for a shim to support getAll in other browsers, which seems to work well enough in Chrome. The code is repeated below for posterity:

(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./axemclion/IndexedDBShim/blob/gh-pages/src/IDBRequest.js
    IDBRequest = function () {
        this.onsuccess = null;
        this.readyState = "pending";
    };
    // https://github./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;
    }
}());
发布评论

评论列表(0)

  1. 暂无评论