0
    this.init = function (onupgradeneeded, onsuccess) {

        var openRequest = indexedDB.open(dbName);

        openRequest.onupgradeneeded = function (e) {

            db = e.target.result;

            if (!db.objectStoreNames.contains(objectStoreName)) {

                console.log('Creating the ' + objectStoreName + ' objectstore');

                db.createObjectStore(objectStoreName, { keyPath: "id", autoIncrement: true });

            }

        };

        openRequest.onsuccess = function (e) {

            db = e.target.result;

            db.onerror = function (event) {
                // Generic error handler for all errors targeted at this database requests
                console.log("Database error: " + event.target.errorCode);
            };
        };

    };

呼び出し元:

var idb = new Demo.IndexedDB();
idb.init();

init 関数が実行されると、最終的にopenRequest.onupgradeneededまたはになりopenRequest.onsuccessます。

私が知りたいのは、両方の関数で呼び出される汎用コールバック関数を作成できるかどうかです。したがって、実行される2つの関数のどちらに関係なく、使用することでいつ完了したかを知ることができます

idb.init(function(){
    //onupgradeneeded or onsuccess completed
});

アイデアが得られることを願っています。

ありがとう

4

1 に答える 1

4

どちらの場合も、1つのコールバック関数を渡して、その1つのコールバックを呼び出すだけです。

this.init = function (onFinish) {

    var openRequest = indexedDB.open(dbName);

    openRequest.onupgradeneeded = function (e) {

        db = e.target.result;

        if (!db.objectStoreNames.contains(objectStoreName)) {

            console.log('Creating the ' + objectStoreName + ' objectstore');

            db.createObjectStore(objectStoreName, { keyPath: "id", autoIncrement: true });

        }
        onFinish();

    };

    openRequest.onsuccess = function (e) {

        db = e.target.result;

        db.onerror = function (event) {
            // Generic error handler for all errors targeted at this database requests
            console.log("Database error: " + event.target.errorCode);
        };
        onFinish();
    };

};
于 2012-08-11T23:47:42.233 に答える