2

DB を開いて、その中でトランザクションを実行しています。ここにコードがあります

var OLC = {}
// Initialising the window.IndexedDB Object
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange;
window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;
var db = null;

OLC.indexedDB = {};
OLC.indexedDB.db = null;
OLC.indexedDB.version = null;

OLC.indexedDB.open = function(type) {
    // opening OLCMail DB
    try {
        var dbOpenRequest = window.indexedDB.open("OLCMail");
        dbOpenRequest.onsuccess = function(event){
            OLC.indexedDB.db = dbOpenRequest.result;
            db = dbOpenRequest.result; 
            OLC.indexedDB.version = db.version;
            var thisDB = db; // Need to create this variable since the variable db is assigned to other things later
            db.onversionchange = function(e){
              console.log("Version change triggered, so closing database connection", e.oldVersion, e.newVersion, thisDB);
              thisDB.close();
            };
        console.log("Database Opened", db, event);
        };

        // Creating objectstore "MailHeaders"
        dbOpenRequest.onupgradeneeded = function(e){
            console.log("Database upgrade needed");
            db = dbOpenRequest.result;
            var transaction = dbOpenRequest.transaction;

....等々

次に、トランザクションを作成してDBから値を取得することを目的としたloadOfflineMailsメソッドがあります

OLC.indexedDB.loadOfflineMails = function () {
    console.log("Opening a new transaction.");
    try {
        var db = OLC.indexedDB.db;
        console.log(OLC.indexedDB.db);
        var objectStore = db.transaction(["MailHeaders"]).objectStore("MailHeaders");
        console.log("Object Store opened", objectStore);

        var cursor = null;

....すぐ

私はそれを実行しています

function _init() {
    OLC.indexedDB.open();
    OLC.indexedDB.loadOfflineMails();
}

window.addEventListener("DOMContentLoaded", _init, false);

私が抱えている問題は、OLC.indexedDB.loadOfflineMails(); を呼び出すと、OLC.indexedDB の db プロパティが null を示しているため、トランザクションが完了していません。

しかし、loadOfflineMails() のトランザクション ステートメントの前に値をアラートすると、コードは正常に動作します。元。

OLC.indexedDB.loadOfflineMails = function () {
        console.log("Opening a new transaction.");
        try {
                    alert("ANY THING");
            var db = OLC.indexedDB.db;
            console.log(OLC.indexedDB.db);
            var objectStore = db.transaction(["MailHeaders"]).objectStore("MailHeaders");
            console.log("Object Store opened", objectStore);

            var cursor = null;

アラート ステートメントがどのようにして null db プロパティを IDBDatabase オブジェクトに変換するのか、まだわかりません。私はconsole.logを試しましたが、アラートの代わりに遅延が発生しましたが、何も機能しません.alert()のようなものは奇跡を引き起こします.

参考までに: 私は console.log(OLC.indexedDB.db) を alert() の前後の両方で実行しました。

4

1 に答える 1

3

OLC.indexedDB.db プロパティが null であるため、2 番目の関数を呼び出したときにデータベースがまだ作成されていません。データベースのオープンはコールバック関数で実行され、残りのコードは順次実行されます。dbOpenRequest.onsuccess 関数の最後で loadOfflineMails 関数を実行するようにコードを変更する必要があります。

このコードを変更します。

function _init() {
    OLC.indexedDB.open();
    OLC.indexedDB.loadOfflineMails();
}

これに:

function _init() {
    OLC.indexedDB.open(null,OLC.indexedDB.loadOfflineMails);
}

2 番目の関数をパラメータとしてデータベースを開く関数に渡すと、DB で実行されるように初期化されます。

Open Db ラッパーを次のように変更する必要があります。

OLC.indexedDB.open = function(type, callback) {
    // opening OLCMail DB
    try {
        var dbOpenRequest = window.indexedDB.open("OLCMail");
        dbOpenRequest.onsuccess = function(event){
            /*your code*/
            callback();//execute the callback function after db init is complete
        }
     }
}
于 2012-09-19T18:00:34.683 に答える