2012-08-11 9 views
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); 
      }; 
     }; 

    }; 

에 의해 불려갑니다.일반적인 비동기 콜백 함수는

두 함수에서 호출 된 제네릭 콜백 함수를 만들 수 있는지 알고 싶습니다. theyre를가

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

당신이 아이디어를 얻을 희망 사용하여 수행 그래서 관계없이 두 가지 기능을하는의 실행이 난 그렇지 않으면 병이 정교한 알 수 있습니다.

감사

+0

그냥 하나 개의 인수를 받아 모두 콜백에 전달 된 인수를 호출 .init''합니다. –

+0

@FelixKling 방금 알아 챘습니다. 때로는 생각보다 쉽게 ​​:-) 감사합니다 – Johan

답변

4

그냥 두 경우 모두 그 하나의 콜백을 하나의 콜백 함수에 전달하고 전화 :

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(); 
    }; 

}; 
+0

감사합니다. 때로는 생각보다 쉽게 ​​:-) – Johan