2012-07-25 2 views
1

아래의 getCursor_ 기능에서 IndexedDb이 열렸는지 확인한 후 다시 실행하지 않으면 기능을 확인하십시오. getCursor_이 올바르게 실행됩니다. 그러나 이러한 호출은 모두 비동기식이므로 데이터베이스 열기가 완료되기 전에 함수를 실행하면 실패합니다.언제 indexedDB에서 연결을 열 었는지 알 수 있습니까?

이 코드는 별도의 프로세스에서 실행되어 ixDbRequest 실행이 완료되지 않는

var ixDb; 
var ixDbRequest; 

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

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

getCursor_ 함수 아래 잘 작동한다. 테스트 방법을 알아 냈지만 열린 데이터베이스 요청이 아직 실행중인 인스턴스에서 기다리는 방법을 모르겠습니다. 아래

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의 대답은 확실히 궤도에 저를 도왔다. 그러나 함수 호출이 비동기 적이기 때문에 "성공"이벤트가 마침내 발생하면 실제로 커서를 사용할 수 있도록 콜백을 추가해야하므로 결국 요청 된 IndexedDb 커서를 가져올 수있었습니다. 마지막 작업 기능 (위의 부 논리를 제거하기 위해 약간-고려 다시 아래에 "만약 (! 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

변경하여하여 addEventListener 라인 에!

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

감사 이것은 확실히 올바른 방향으로 날을 보낸 나는 위의 업데이트에 나의 마지막 작업 기능을 넣어 알려 주시기 바랍니다, 당신이 다른 생각이있는 경우..! –

관련 문제