我什么时候可以告诉我在indexedDB中打开了一个连接?

时间:2012-07-25 02:12:23

标签: javascript indexeddb

在下面的getCursor_函数中,请说明我如何确定IndexedDb是否已打开,如果没有重新运行功能? getCursor_正确运行。但是,由于所有这些调用都是异步的,因此在数据库完成打开之前执行时函数会失败。

此代码在单独的过程中执行:

var ixDb;
var ixDbRequest;

ixDbRequest = window.indexedDB.open(dbName, dbVersion);

ixDbRequest.onsuccess = function (e) {
            ixDb = ixDbRequest.result || e.result;
}; 

以下getCursor_函数正常工作,除非ixDbRequest尚未完成执行。我想出了如何测试,但我不知道如何在开放数据库请求仍在运行的实例中等待。

function getCursor_(objectStoreName) {
    if (!ixDb) {
        if (ixDbRequest) {
            // "Database is still opening.  Need code to wait or re-run 
            // once completed here.
            // I tried the following with no luck:
            ixDbRequest.addEventListener ("success",
                getCursor_(objectStoreName), false); 
            return;
        }
    }

    var transaction = ixDb.transaction(objectStoreName, 
                                       IDBTransaction.READ_ONLY);
    var objectStore = transaction.objectStore(objectStoreName);  
    try{
        var request = objectStore.openCursor(); 
        return request;
    }
    catch(e){
        console.log('IndexedDB Error' + '(' + e.code + '): ' + e.message);
    }
}

更新如下:

@Dangerz的答案肯定帮助我走上正轨。但是,由于函数调用是异步的,我最终还是必须添加一个回调,以便在“success”事件最终触发后实际能够使用游标,并且我能够获得所请求的IndexedDb游标。最终的工作函数如下(稍微重新考虑以消除“if(!ixDb)”之上的否定逻辑。如果有人看到改进的空间,我完全愿意接受建议!

//****************************************************************************
//Function getCursor - Returns a cursor for the requested ObjectStore
//  objectStoreName  - Name of the Object Store / Table "MyOsName" 
//         callback  - Name of the function to call and pass the cursor 
//                     request back to upon completion.
//                     Ex. getCursor_("ObjectStore_Name", MyCallBackFunction);
//                     Function MyCallBackFunction(CursorRequestObj) { 
//                     CursorRequestObj.onsuccess = 
//                               function() {//do stuff here};
//                     }
//****************************************************************************
function getCursor_(objectStoreName, callback) {
  //The the openCursor call is asynchronous, so we must check to ensure a 
  // database connection has been established and then provide the cursor 
  // via callback. 
  if (ixDb) {
      var transaction = 
        ixDb.transaction(objectStoreName, IDBTransaction.READ_ONLY);
      var objectStore = transaction.objectStore(objectStoreName); 

      try{
        var request = objectStore.openCursor(); 
        callback(request);
        console.log('ixDbEz: Getting cursor request for ' 
                    + objectStoreName + ".");
      }
      catch(e){
        console.log('ixDbEz Error' + ' getCursor:(' 
                    + e.code + '): ' + e.message);
      } 
  }
  else { 
    if (ixDbRequest) {
      ixDbRequest.addEventListener ("success"
                    , function() { getCursor_(objectStoreName, callback); }
                    , false);
    }
  }
}

1 个答案:

答案 0 :(得分:1)

将addEventListener行更改为:

ixDbRequest.addEventListener ("success", function() { getCursor_(objectStoreName) }, false);