mirror of
https://scm.univ-tours.fr/22107988t/rappaurio-sae501_502.git
synced 2025-08-29 22:25:58 +02:00
104 lines
2.8 KiB
JavaScript
104 lines
2.8 KiB
JavaScript
/**
|
|
* @name CeL function for Indexed Database API
|
|
* @fileoverview 本檔案包含了 Indexed Database API 用的 functions。
|
|
*
|
|
* @since
|
|
* @example <code>
|
|
|
|
CeL.run('data.statistics', function() {
|
|
// table / objectStore
|
|
// schema: use createIndex(name, keyPath, optionalParameters)
|
|
// schema: {key:key name/key function, unique: false, multiEntry: false}
|
|
// keyPath: key name @ objectStore
|
|
// keyPath: how to get the key of inputted object.
|
|
var store = new CeL.indexedDB(database_name, {
|
|
store : store_name,
|
|
version : 1,
|
|
schema : [ key / column0, column1, column2 ],
|
|
keyPath : 'id',
|
|
autoIncrement : true,
|
|
error : function() {
|
|
}
|
|
});
|
|
store.add();
|
|
store.get();
|
|
store.delete();
|
|
store.forEach(function() {
|
|
});
|
|
store.db.close();
|
|
});
|
|
|
|
* </code>
|
|
*
|
|
* @see http://www.w3.org/TR/IndexedDB/
|
|
* https://developer.mozilla.org/en-US/docs/IndexedDB
|
|
* https://developer.mozilla.org/zh-TW/docs/Web/API/IndexedDB_API/Using_IndexedDB
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
// 不採用 if 陳述式,可以避免 Eclipse JSDoc 與 format 多縮排一層。
|
|
typeof CeL === 'function' && CeL.run({
|
|
// module name
|
|
name : 'data.indexedDB',
|
|
|
|
require : '',
|
|
|
|
// 設定不匯出的子函式。
|
|
// no_extend : '*',
|
|
|
|
// 為了方便格式化程式碼,因此將 module 函式主體另外抽出。
|
|
code : module_code
|
|
});
|
|
|
|
function module_code(library_namespace) {
|
|
|
|
if (false) {
|
|
var indexedDB;
|
|
if (typeof indexedDB !== 'object')
|
|
indexedDB = window.indexedDB || window.mozIndexedDB
|
|
|| window.webkitIndexedDB || window.msIndexedDB;
|
|
}
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
function IndexedDB(database_name, options) {
|
|
options = library_namespace.setup_options(options);
|
|
|
|
var request = window.indexedDB.open(database_name, options.version);
|
|
|
|
// var db = event.target.result;
|
|
request.onsuccess = options.onsuccess;
|
|
request.onerror = options.onerror;
|
|
|
|
var _this = this;
|
|
request.onupgradeneeded = function(event) {
|
|
// request === event.target
|
|
this.db = event.target.result;
|
|
console.log(this.db);
|
|
this.db.onerror = function(event) {
|
|
// Generic error handler for all errors targeted at this
|
|
// database's requests!
|
|
console.error("Database error: " + event.target.errorCode);
|
|
};
|
|
var store_name = options.store, objectStore;
|
|
if (!this.db.objectStoreNames.contains(store_name)) {
|
|
objectStore = this.db.createObjectStore(store_name, {
|
|
keyPath : options.keyPath,
|
|
autoIncrement : options.autoIncrement
|
|
});
|
|
}
|
|
};
|
|
|
|
}
|
|
|
|
// ------------------------------------------------------------
|
|
TODO
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
return IndexedDB;
|
|
}
|