2012-07-18 20 views
10

我已经看到多个JavaScript例子您可以在预先存在的表格上使用createIndex而不致电createObjectStore?我想这里真正的问题是如何获得对objectStore的引用而不使用createObjectStore添加索引到预先存在的ObjectStore在IndexedDB的使用JavaScript

我试过,没有运气以下几个变化:

var objectStore = window.IDBTransaction.objectStore(ObjectStoreName); 
var index = objectStore.createIndex(ixName, fieldName, { unique: unique, multiEntry: multiEntry }); 
+0

request.onupgradeneeded =函数(evt)var objectStore = evt.currentTarget.transaction.objectStore(“people”); objectStore.createIndex(“city”,“city”,{unique:false}); }; //快速回答 – Garry 2016-04-01 00:03:36

回答

4

你这样做时onupgradeneeded,这应该是你正在对象存储在首位在同一个地方。在那里,你可以做任何适当的行动来升级它,比如创建一个新的索引。 Here is an example.

+0

onupgradeneeded在Chrome中甚至没有启动。它是最新的W3规范的一部分。如果我能够弄清楚如何使用事件以外的其他东西来获取对象库引用,那将会很好。 – 2012-07-18 19:48:45

+0

它们都不能在IE或Opera或Safari中运行,所以使用IndexedDB意味着你牺牲了一些跨浏览器的兼容性。 Chrome会很快支持'onupgradeneeded'。我想我知道他们的目标是Chrome 22,所以也许这几个月都会在Chrome上运行。在此之前,您可以通过执行类似[this]的操作来破解它(http://blog.nparashuram.com/2012/05/indexeddb-setversion-vs-onupgradeneeded.html)。 – dumbmatter 2012-07-18 19:54:43

+0

而且由于我之前并不清楚,我想......当升级数据库时,只能混淆对象存储。没有其他解决方案。 ['onupgradeneeded'“非常重要,因为它是代码中唯一可以创建对象存储和索引的地方。”](https://developer.mozilla.org/en/IndexedDB/Using_IndexedDB) – dumbmatter 2012-07-18 19:56:17

2

Chrome目前不支持onupgradeneeded事件。如果你想使用旧的或新的W3规范去的ObjectStore的参考,这是利用旧setVersion命令,或者通过新的onupgradeneeded事件通过的onSuccess事件一个可能的解决方法:

var ixDb; 
var ixDbRequest; 
var ixDbVersionTansaction; 

//Check to see if we have a browser that supports IndexedDB 
if (window.indexedDB) { 

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

//For browsers like chrome that support the old set version method 
ixDbRequest.onsuccess = function (e) { 

    ixDb = ixDbRequest.result || e.result; 

    if (typeof ixDb.setVersion === "function") { 

     //Put your version checking logic here 

     if (oldVersion < newVersion) { 
      var verRequest = ixDb.setVersion(newVersion); 

      verRequest.onerror = function (e) { 
       //handling error logic here 
      } 

      verRequest.onsuccess = function (e) { 
       //Get a reference to the version transaction 
       //from the old setVersion method. 
       ixDbVersionTansaction = verRequest.result; 
       //Create database using function provided by the user. 
       UserFunction(); 
      } 
     } 
    } 
}; 

ixDbRequest.onupgradeneeded = function (e) { 
    //FF uses this event to fire the transaction for upgrades. 
    //All browsers will eventually use this method. Per - W3C Working Draft 24 May 2012 
    ixDb = ixDbRequest.result || e.currentTarget.result; 

    //Get a reference to the version transaction via the onupgradeneeded event (e) 
    ixDbVersionTansaction = e.currentTarget.transaction; 

    //Create database using function provided by the user. 
    UserFunction(); 
}; 

UserFunction(){ 
    //ObjectStore is accessed via ixDbVersionTansaction variable 
    // in either instance (transaction..objectStore("ObjectStoreName")) 
    var ObjectStore = ixDbVersionTansaction.objectStore("ObjectStoreName"); 
    var index = ObjectStore.createIndex("ixName", "fieldName"); 
} 
相关问题