YDN-DB - 删除数据库,然后在页面加载/重新加载时重新创建和加载,但在刷新时不加载
Posted
技术标签:
【中文标题】YDN-DB - 删除数据库,然后在页面加载/重新加载时重新创建和加载,但在刷新时不加载【英文标题】:YDN-DB - Delete DB then recreate and load when page is loaded/reloaded but not when refreshed 【发布时间】:2016-10-07 22:10:36 【问题描述】:我正在转换一些 IndexedDB 代码以使用 YDN-DB,并且需要一些帮助来转换它。如果页面已加载或重新加载,则此代码实质上会删除 DB,但在刷新时不会。 我将如何使用 YDN-DB 实现这一目标?
var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;
var db;
window.refreshing = false;
$(document).ready(function()
if(window.performance)
if(performance.navigation.type == 0 )
// The db already exists so delete it and re-create it so we don't have stale records.
deleteDB();
else
window.refreshing = true;
open();
);
function deleteDB()
var request = indexedDB.deleteDatabase("DocsDB");
request.onsuccess = function()
open();
request.onerror = function(event)
throw "Error in deleteDB.";
function open()
var request = indexedDB.open("DocsDB");
var upgraded = false;
request.onupgradeneeded = function(evt)
upgraded = true;
var dbnew = evt.target.result;
dbnew.onerror = function(event)
console.log("IndexedDB error: " + evt.target.error.code);
;
var objectStore = dbnew.createObjectStore(
"docs", keyPath: "id", autoIncrement: true );
objectStore.createIndex("docname", "DOC_NAME", unique: false );
objectStore.createIndex("printdate", "PRINT_DATE", unique: false );
request.onsuccess = function(evt)
db = request.result;
if (!upgraded && !window.refreshing)
throw "Not upgraded";
request.result.onversionchange = function(e)
if (e.newVersion === null) // An attempt is made to delete the db
e.target.close(); // Manually close our connection to the db
;
if(typeof db != 'undefined' && !window.refreshing)
// We load the store with the records here, attempting to add records via ajax doesn't work
// looks like we'll ahve to load them via ajax before initialization?
var store = getObjectStore(db);
for (i=0; i<documents["Rows"].length-1;i++)
store.add(documents["Rows"][i]);
request.onerror = function()
throw "Error in open";
function getObjectStore(db, mode = 'readwrite')
if(typeof db != 'undefined')
var tx = db.transaction('docs', mode);
return tx.objectStore('docs');
else
return null;
【问题讨论】:
【参考方案1】:要删除数据库使用,ydn.db.deleteDatabase("DocsDB")
。
【讨论】:
是的,我知道该怎么做,但是是否有一个事件,例如onsuccess
用于删除数据库后能够调用 open()
?
好的,在文档中找到了 .done()
/ .fail()
承诺。正在尝试使用 .onsuccess()
/ .onerror()
将发布一个示例供您与您的库一起使用。
我将修改后的示例添加到您的 GitHub 存储库上的票证中。 github.com/yathit/ydn-db/issues/105以上是关于YDN-DB - 删除数据库,然后在页面加载/重新加载时重新创建和加载,但在刷新时不加载的主要内容,如果未能解决你的问题,请参考以下文章