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

    }; 

被调用。通用异步回调函数

我想知道的是,如果它可能创建一个通用的回调函数,在函数中调用。所以不管哪两个功能的运行,我可以知道什么时候吗更有用

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

希望你的想法做,否则生病阐述。

感谢

+0

只是要'.init'接受一个参数,调用回调都传入的参数。 – 2012-08-11 23:46:10

+0

@FelixKling刚刚注意到。有时它比你想象的更容易:-)谢谢 – Johan 2012-08-11 23:47:30

回答

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 2012-08-11 23:50:01