2012-12-26 46 views
1

我使用下面的代碼來讀取索引數據庫的數據並將其保存在變量allDownloadContent存儲檢索從索引數據庫的數據給一個變量

ereaderdownload.indexedDB.getAllTodoItems = function() { 

    /*var todos = document.getElementById("todoItems"); 
    todos.innerHTML = ""; 
    */ 
    var db = ereaderdownload.indexedDB.db; 
    var trans = db.transaction(["downloadcontent"], "readwrite"); 
    var store = trans.objectStore("downloadcontent"); 
    var request = store.get(0); 

    request.onsuccess = function(e) { 
     console.log(e.target.result); 
    }; 

    // Get everything in the store; 
    var cursorRequest = store.openCursor(); 
    cursorRequest.onsuccess = function(e) { 
     var result = e.target.result; 
     if(!!result == false) 
      return; 
     allDownloadContent.push(result); 
     result.continue(); 
    }; 
    alert("content "+allDownloadContent[0]); 
    cursorRequest.onerror = ereaderdownload.indexedDB.onerror; 
    }; 

當我打電話getAllTodoItems方法從我得到另一個JavaScript文件一個警告消息內容不確定

因爲cursorRequest.onsuccess方法執行異步我越來越不確定。

我不能使用網絡工作者,因爲它在chrome中不受支持。

我在JQuery中試過諾言。我仍然收到相同的警報消息。

請幫我解決這個問題。

+0

你是什麼意思的網絡工作者不支持在Chrome? http://caniuse.com/webworkers – KKetch

+0

我的意思是同步indexeddb api – aaviss

+0

我想你想在你編寫'return'的回調中提醒。那時數據可用。 – pimvdb

回答

3

至於現在所有的瀏覽器只支持Indexed-db ASync API,你需要做的是將事件監聽器添加到事務oncomplete事件中。光標關閉時,此事件將觸發。從那裏你可以返回到你的代碼:

trans.oncomplete = function (event) { 
    console.log('transaction completed'); 
    yourFunction(); 
}; 
+1

你給了我一個很好的解決方案。非常感謝Deni。優秀... – aaviss