htm5本地存储方案——indexdb的封装

不BB直接上代码

/*封装IndexdDB*/
var localDatabase = {

};
localDatabase.dbName = "yiliDB";
localDatabase.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
localDatabase.db = null;
localDatabase.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange;
localDatabase.IDBCursor = window.IDBCursor || window.webkitIDBCursor;
localDatabase.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;
localDatabase.indexedDB.onerror = function(e) {
    console.log("Database error: " + e.target.errorCode);
};
//删除数据库
localDatabase.deleteIndexdDB = function() {
    var deleteDbRequest = localDatabase.indexedDB.deleteDatabase(localDatabase.dbName);
    deleteDbRequest.onsuccess = function(event) {
        console.log('Database deleted');
        localDatabase.db = null;
    };
    deleteDbRequest.onerror = function(e) {
        console.log("Database error: " + e.target.errorCode);

    };
};
//打开数据库
localDatabase.openIndexdDB = function(callback) {
    console.log("init IndexdDB");
    try {
        var openRequest = localDatabase.indexedDB.open(localDatabase.dbName);
        openRequest.onerror = function(e) {
            callback("Database error: " + e.target.errorCode);
        };
        openRequest.onsuccess = function(event) {
            localDatabase.db = openRequest.result;
            console.log("Open IndexdDB success.");
            callback("success");
            localDatabase.db.onversionchange = function() {
                console.log("DB close");
                localDatabase.db.close();
            };

        };
        openRequest.onupgradeneeded = function(evt) {
            console.log('Creating object stores');
            var MessageStore = evt.currentTarget.result.createObjectStore("message");
            MessageStore.createIndex("userid", "userid", {
                unique: false
            });

        };

    } catch(e1) {

        console.log(e1);

    }

};

再来个测试的例子

//添加消息
localDatabase.addMessageToIndexDB = function(obj, userid) {
    try {
        console.log(localDatabase.db);
        if(localDatabase != null && localDatabase.db != null) {
            var transaction = localDatabase.db.transaction("message", "readwrite");
            var store = transaction.objectStore("message");
            var request = store.get(userid);
            request.onsuccess = function(e) {
                var result = e.target.result;
                var exist = 0;
                if(typeof result != 'undefined') {
                    for(var i = 0; i < result.length; i++) {
                        if(result[i].userid == obj.userid) {
                            result[i] = obj;
                            exist++;
                            break;
                        }
                    }
                    if(!exist) {
                        result.push(obj);
                    }
                    store.put(result, userid);
                } else {
                    store.add([obj], userid);
                }
            };
        }
    } catch(e) {
        console.log(e)
    }
};
//获取消息
localDatabase.getMessage = function(userid, callback) {
    try {

        if(localDatabase != null && localDatabase.db != null) {

            var store = localDatabase.db.transaction("message").objectStore("message");
            //            var index = store.index("userid");
            //            var request = index.openCursor(IDBKeyRange.only(userid));
            request = store.get(userid);
            request.onsuccess = function(res) {
                var result = res.target.result;
                if(typeof result != 'undefined') {
                    callback(result);
                } else {
                    callback(null);
                }

            };

        }
    } catch(e) {

        console.log(e);

    }

};

 

posted @ 2017-08-16 12:16  HuTiger  阅读(1628)  评论(0编辑  收藏  举报