IndexDB.js code:
var pkIndexData = WinJS.Class.define(
function (config) {
this._init(config);
},
{
dbRequest: null,
dbCreated: false,
'config':{
dbName: 'pkIndex.db',
dbVersion: '1',
dbTable: 'table1',
dbIndexKey: 'id',
dbIndexKeyAutoIncrement: true
},
'_init': function (config) {
$.extend(this.config, config);
var dbRequest = window.indexedDB.open(this.config.dbName, this.config.dbVersion);
// Add asynchronous callback functions.
dbRequest.onerror = function () { WinJS.log && WinJS.log("Error opening database.", "sample", "error"); };
dbRequest.onblocked = function () { WinJS.log && WinJS.log("Database access blocked.", "sample", "error"); };
dbRequest.onupgradeneeded = this.dbGradeNeeded;
dbRequest.onsuccess = this.dbSuccess;
},
onDbCreateCompleted: function (evt) {
this.dbCreated = true;
},
dbGradeNeeded: function (evt) {
if (this.db) {
this.db.close();
}
this.db = evt.target.result;
var self = this;
evt.target.transaction.oncomplete = function (evt) {
self.onDbCreateCompleted(evt);
self = null;
};
this.db.createObjectStore(this.config.dbTable, { keyPath: this.config.dbIndexKey, autoIncrement: this.config.dbIndexKeyAutoIncrement });
},
dbSuccess:function(evt) {
// If the database was previously loaded, close it. Closing the database keeps it from becoming blocked for later deletes operations.
if (this.dbRequest) {
this.dbRequest.close();
}
this.dbRequest = evt.target.result;
if (this.db.objectStoreNames.length === 0) {
WinJS.log && WinJS.log("Database schema does not exist. Complete the first two scenarios before continuing.", "sample", "error");
this.db.close();
this.db = null;
window.indexedDB.deleteDatabase(this.config.dbName, this.config.dbVersion);
} else {
this.onDbCreateCompleted(evt);
}
},
readUrlListData: function (cb) {
if (!this.dbCreated || !cb)
return false;
// Declare arrays to hold the data to be read.
var urllist = [];
// Create a transaction with which to query the IndexedDB.
var trans = this.db.transaction([this.config.dbTable], "readonly");
// Set the event callbacks for the transaction.
trans.onerror = function () { WinJS.log && WinJS.log("Error reading data.", "sample", "error"); };
trans.onabort = function () { WinJS.log && WinJS.log("Reading of data aborted.", "sample", "error"); };
trans.oncomplete = function () {
if (cb)
cb(urllist);
};
var urllistCursorRequest = txn.objectStore(this.config.dbTable).openCursor();
urllistCursorRequest.onsuccess = function (e) {
var cursor = e.target.result;
if (cursor) {
var data = cursor.value;
urllist[data.url] = data.status;
cursor.continue();
}
};
},
writeUrlListData: function (datas) {
if (!this.dbCreated)
return false;
var trans = this.db.transaction([this.config.dbTable], "readwrite");
// Set the event callbacks for the transaction
trans.onerror = function (evt) { WinJS.log && WinJS.log("Error writing data.", "sample", "error"); };
trans.onabort = function (evt) { WinJS.log && WinJS.log("Writing of data aborted.", "sample", "error"); };
trans.oncomplete = function () {
WinJS.log && WinJS.log("Changes saved to database.", "sample", "status");
};
var store = trans.objectStore(this.config.dbTable);
for (var key in datas) {
store[key] = datas[key];
}
}
});
gameData.js:
var GameData = WinJS.Class.derive(
pkIndexData, function (config) {
this._init(config);
},
{
onDbCreateCompleted: function (evt) {
this.dbCreated = true;
},
dbGradeNeeded: function (evt) {
if (this.db) {
this.db.close();
}
this.db = evt.target.result;
evt.target.transaction.oncomplete = function (evt) {
GameManager.gameData.onDbCreateCompleted(evt);
};
this.db.createObjectStore("urllist", { keyPath: 'url', autoIncrement: false });
},
dbSuccess:function(evt) {
// If the database was previously loaded, close it. Closing the database keeps it from becoming blocked for later deletes operations.
if (this.dbRequest) {
this.dbRequest.close();
}
this.dbRequest = evt.target.result;
if (this.db.objectStoreNames.length === 0) {
WinJS.log && WinJS.log("Database schema does not exist. Complete the first two scenarios before continuing.", "sample", "error");
this.db.close();
this.db = null;
window.indexedDB.deleteDatabase(this.dbName, this.dbVersion);
} else {
this.onDbCreateCompleted(evt);
}
},
readUrlListData: function (cb) {
if (!this.dbCreated || !cb) {
if (cb)
cb(null);
return;
}
// Declare arrays to hold the data to be read.
var urllist = [];
// Create a transaction with which to query the IndexedDB.
var trans = this.db.transaction(["urllist"], "readonly");
var cbfun = function () {
if (cb)
cb(urllist);
};
// Set the event callbacks for the transaction.
trans.onerror = cbfun;
trans.onabort = cbfun;
trans.oncomplete = cbfun;
var urllistCursorRequest = txn.objectStore("urllist").openCursor();
urllistCursorRequest.onsuccess = function (e) {
var cursor = e.target.result;
if (cursor) {
var data = cursor.value;
urllist[data.url] = data;
cursor.continue();
}
};
},
addUrlListData: function (item) {
if (!this.dbCreated)
return false;
var trans = this.db.transaction(["urllist"], "readwrite");
// Set the event callbacks for the transaction
trans.onerror = function (evt) { WinJS.log && WinJS.log("Error writing data.", "sample", "error"); };
trans.onabort = function (evt) { WinJS.log && WinJS.log("Writing of data aborted.", "sample", "error"); };
trans.oncomplete = function () {
WinJS.log && WinJS.log("Changes saved to database.", "sample", "status");
};
var store = trans.objectStore("urllist");
var url = item.url;
item.url = null;
store[url] = item;
},
writeUrlListData: function (urllist) {
if (!this.dbCreated)
return false;
var trans = this.db.transaction(["urllist"], "readwrite");
// Set the event callbacks for the transaction
trans.onerror = function (evt) { WinJS.log && WinJS.log("Error writing data.", "sample", "error"); };
trans.onabort = function (evt) { WinJS.log && WinJS.log("Writing of data aborted.", "sample", "error"); };
trans.oncomplete = function () {
WinJS.log && WinJS.log("Changes saved to database.", "sample", "status");
};
var store = trans.objectStore("urllist");
for (var key in urllist) {
store[key] = urllist[key];
}
}
});
error:
“WWAHost.exe”(脚本): 已加载“脚本代码(MSAppHost/1.0)”。
引发了异常,行 5357,列 6,在 ms-appx://42252pkrss.pkmap/js/jquery/jquery.min.js 中
0x800a139e - JavaScript 运行时错误: SyntaxError
未经处理的异常,行 19,列 9,在 ms-appx://42252pkrss.pkmap/js/indexData.js 中
0x800a139e - JavaScript 运行时错误: InvalidAccessError
程序“[8092] WWAHost.exe”已退出,返回值为 -1 (0xffffffff)。
fixed:
window.indexedDB.open("pkDB","1");
=>
window.indexedDB.open("pkDB",1);
No comments:
Post a Comment