在对象库中填充数据,从IndexedDB中的其他对象库中删除数据

时间:2014-10-13 04:00:05

标签: indexeddb

我需要在IndexedDB数据库中创建七个对象库。我逐个创建它们并在每次创建事务完成后填充它们。我从ajax调用返回的数据填充每个对象存储。

在JS执行结束时,我可以看到创建了所有七个对象存储,但只有最后一个存在数据。在调试模式下,我观察到每个人都被填充,但同时,我不知道以前填充的对象存储中的数据是如何被删除的。

谢谢。 Nandita

2 个答案:

答案 0 :(得分:0)

以下是完整的JavaScript代码:

function openAddressDB(){

if (!window.indexedDB) {
  window.alert("Your browser doesn't support a stable version of IndexedDB. Can't store address data in web storage");        
} else {
  // Let us open address database
  var request = window.indexedDB.open(Address_DB_Name, Address_DB_Version);

  //Sucess event handler
  request.onsuccess = function(event) {
    addressDb = request.result; 

    refreshAddressList();      

    // Generic error handler for all errors targeted at this database's requests!
    addressDb.onerror = function(event) {          
      alert(Address_DB_Name + " error: " + event.target.errorCode);
    };
  };


  // If some other tab is loaded with the database, then it needs to be closed
  // before we can proceed.
  request.onblocked = function(event) {  
    alert("Need to update "+Address_DB_Name+" database, please close all other tabs with this site open!");
  }; 

  //Error event handler
  request.onerror = function(event) {
     alert("Open "+Address_DB_Name+" failed:" + event.target.errorCode );
  };

  //upgrade event handler
  request.onupgradeneeded = function (evt) { 
    addressDb =  evt.currentTarget.result;

    //delete object store if database is upgrading
    if (event.oldVersion != 0){

      if(addressDb.objectStoreNames.contains(Address1_Store)) {
         addressDb.deleteObjectStore(Address1_Store);
      }

      if(addressDb.objectStoreNames.contains(Address2_Store)) {                     
        addressDb.deleteObjectStore(Address2_Store);
      }

      if(addressDb.objectStoreNames.contains(Address3_Store)) {
        addressDb.deleteObjectStore(Address3_Store);
      }
      if(addressDb.objectStoreNames.contains(Town_City_Store)) {
        addressDb.deleteObjectStore(Town_City_Store);
      }
      if(addressDb.objectStoreNames.contains(County_Store)) {
        addressDb.deleteObjectStore(County_Store);
      }
      if (addressDb.objectStoreNames.contains(State_Store)) {
       addressDb.deleteObjectStore(State_Store);
      }
      if  (addressDb.objectStoreNames.contains(Post_Code_Store)) {
       addressDb.deleteObjectStore(Post_Code_Store);
      }
      if  (addressDb.objectStoreNames.contains(Country_Store)) {
       addressDb.deleteObjectStore(Country_Store); 
      }
      if  (addressDb.objectStoreNames.contains(Region_Store)) {
       addressDb.deleteObjectStore(Region_Store);                      
      }
    }     


    //creating object store  and store data

    //Address 1               
    $("#addressview").find("[name='address1id']").each(function(index){ 
        address1Store =addressDb.createObjectStore(Address1_Store,{ keyPath: "id" });
        // Create an index to search on 
        address1Store.createIndex("parentAddressListValueId", "parentAddressListValueId", { unique: false });
        address1Store.transaction.oncomplete = function(event){                   
            loadAddressList('address1id', 'Address1')           
          };
    });      

    //Address 2
    $("#addressview").find("[name='address2id']").each(function(index){               
        address2Store =addressDb.createObjectStore(Address2_Store,{ keyPath: "id" });
        address2Store.createIndex("parentAddressListValueId", "parentAddressListValueId", { unique: false });
        address2Store.transaction.oncomplete = function(event){                     
            loadAddressList('address2id','Address2')           
          };
    }); 



    //Address 3
    $("#addressview").find("[name='address3id']").each(function(index){                    
      address3Store =addressDb.createObjectStore(Address3_Store,{ keyPath: "id" });
      address3Store.createIndex("parentAddressListValueId", "parentAddressListValueId", { unique: false });
      address3Store.transaction.oncomplete = function(event){                
          loadAddressList('address3id','Address3')           
        };
    });    

    //Town/City
    $("#addressview").find("[name='towncityid']").each(function(index){               
      townCityStore =addressDb.createObjectStore(Town_City_Store,{ keyPath: "id" });
      townCityStore.createIndex("parentAddressListValueId", "parentAddressListValueId", { unique: false });
      townCityStore.transaction.oncomplete = function(event){                     
          loadAddressList('towncityid','TownCity')           
        };
    });     

    //county
    $("#addressview").find("[name='countyid']").each(function(index){  
      countyStore =addressDb.createObjectStore(County_Store,{ keyPath: "id" });
      countyStore.createIndex("parentAddressListValueId", "parentAddressListValueId", { unique: false });
      countyStore.transaction.oncomplete = function(event){                  
          loadAddressList('countyid','County')           
        };
    });    

    //state
    $("#addressview").find("[name='stateid']").each(function(index){
      stateStore =addressDb.createObjectStore(State_Store,{ keyPath: "id" });
      stateStore.createIndex("parentAddressListValueId", "parentAddressListValueId", { unique: false });
      stateStore.transaction.oncomplete = function(event){                     
        loadAddressList('stateid','State')           
      };
    });    

    //Postal Code
    $("#addressview").find("[name='postalcodeid']").each(function(index){     
      postalCodeStore =addressDb.createObjectStore(Post_Code_Store,{ keyPath: "id" });
      postalCodeStore.createIndex("parentAddressListValueId", "parentAddressListValueId", { unique: false });
      postalCodeStore.transaction.oncomplete = function(event){                
        loadAddressList('postalcodeid','PostalCode')           
      };
    });    

    //Country 
    $("#addressview").find("[name='countryid']").each(function(index){           
      countryStore =addressDb.createObjectStore(Country_Store,{ keyPath: "id" });
      countryStore.createIndex("parentAddressListValueId", "parentAddressListValueId", { unique: false });
      countryStore.transaction.oncomplete = function(event){           
          loadAddressList('countryid','Country')           
        };
    });   

    //Region
    $("#addressview").find("[name='regionid']").each(function(index){    
      regionStore =addressDb.createObjectStore(Region_Store,{ keyPath: "id" });
      regionStore.createIndex("parentAddressListValueId", "parentAddressListValueId", { unique: false });
      regionStore.transaction.oncomplete = function(event){                    
          loadAddressList('regionid','Region')           
        };
    });

    addressDb.onversionchange = function(event) {
      alert(Address_DB_Name+' database change has occurred; you should refresh this browser window, or close it down and use the other '+ 'open version of this application, wherever it exists.');
    };       

    window.location.reload();
  }; // onupgradeneeded

} //其他 } //函数openAddressDB()

function loadAddressList(fieldName,address_field){
        $就({                类型:“GET”,                url:“/ settings / address_lst_values /”+ address_field,                数据:{},                beforeSend:function(){                   $( '#address_loading')显示()。                },                完成:function(){                   $( '#address_loading')隐藏()。                },                成功:功能(数据,状态,jqXHR)                {
                if(anyAjaxError(data,status,jqXHR)=== false)                 {                   //存储在浏览器对象库中                   var store = getObjectStore(fieldName,'readwrite');
                  for(var i in data){
                    store.add(数据[I]);                   }
                }
                $( '#address_loading')隐藏()。                },                错误:函数(jqXHR,status,errorThrown)                {                 ajaxCallFail(jqXHR,status,errorThrown,“加载时出错”+ address_field +“data”)                }
        }); // ajax调用加载地址列表
} //函数加载地址列表

答案 1 :(得分:0)

我找到了这个问题的原因了 在创建每个对象库之后调用transaction.oncomplete会覆盖它。因此,只有最后分配的函数调用才能执行。这导致只填充最后一个对象存储而不是前一个。