标签:
代码来源于传智播客
1 angular.module(‘indexdb‘, []) 2 .factory(‘IndexdbJs‘, [‘$ionicPopup‘,function ($ionicPopup) { 3 4 window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB; 5 window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction; 6 window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange; 7 window.IDBCursor=window.IDBCursor||window.webkitIDBCursor|| window.msIDBCursor; 8 var db={ 9 dbName: ‘aptdb‘, 10 dbVersion: 2046, //用小数会四舍五入 11 dbInstance: {}, 12 13 errorHandler: function (error) { 14 console.log(‘error: ‘ + error.target.error.message); 15 }, 16 17 open: function (func,fail) { 18 var dbContent = window.indexedDB.open(db.dbName, db.dbVersion); 19 dbContent.onupgradeneeded = db.upgrade; 20 dbContent.onerror = db.errorHandler; 21 dbContent.onsuccess = function (e) { 22 db.dbInstance = dbContent.result; 23 db.dbInstance.onerror = fail; 24 func(); 25 }; 26 }, 27 upgrade: function (e) { 28 var _db = e.target.result, names = _db.objectStoreNames; 29 // 此处可以创建多个表 30 var name = "cart"; 31 if (!names.contains(name)) { 32 _db.createObjectStore( 33 name, 34 { 35 keyPath: ‘goodsId‘, 36 autoIncrement:false 37 }); 38 } 39 }, 40 getObjectStore: function (objectStoreName,mode) { 41 var txn, store;mode = mode || ‘readonly‘; 42 txn = db.dbInstance.transaction([objectStoreName], mode); 43 store = txn.objectStore(objectStoreName); 44 return store; 45 }, 46 add: function (objectStoreName,data,success,fail) { 47 db.open(function () { 48 var store, req, mode = ‘readwrite‘; 49 store = db.getObjectStore(objectStoreName,mode), 50 req = store.add(data); 51 req.onsuccess = success; 52 req.onerror=fail; 53 },fail); 54 }, 55 update: function (objectStoreName,data,success,fail) { 56 db.open(function () { 57 var store, req, mode = ‘readwrite‘; 58 store = db.getObjectStore(objectStoreName,mode), 59 req = store.put(data); 60 req.onsuccess = success; 61 req.onerror=fail; 62 },fail); 63 }, 64 getAll: function (objectStoreName,success,fail) { 65 66 db.open(function () { 67 var 68 store = db.getObjectStore(objectStoreName), 69 cursor = store.openCursor(), 70 data = []; 71 72 cursor.onsuccess = function (e) { 73 var result = e.target.result; 74 if (result && result !== null) { 75 data.push(result.value); 76 result.continue(); 77 } else { 78 success(data); 79 } 80 }; 81 cursor.onerror=fail; 82 83 },fail); 84 }, 85 get: function (id,objectStoreName,success,fail) { 86 db.open(function () { 87 var 88 store = db.getObjectStore(objectStoreName), 89 req = store.get(id); 90 req.onsuccess = function (e){ 91 success(e.target.result); 92 }; 93 req.onerror=fail; 94 }); 95 }, 96 // delete是保留字 97 ‘delete‘: function (id,objectStoreName,success,fail) { 98 db.open(function () { 99 var 100 mode = ‘readwrite‘, 101 store, req; 102 store = db.getObjectStore(objectStoreName,mode); 103 req = store.delete(id); 104 req.onsuccess = success; 105 req.onerror=fail; 106 }); 107 }, 108 deleteAll: function (objectStoreName,success,fail) { 109 db.open(function () { 110 var mode, store, req; 111 mode = ‘readwrite‘; 112 store = db.getObjectStore(objectStoreName,mode); 113 req = store.clear(); 114 req.onsuccess = success; 115 req.onerror=fail; 116 }); 117 } 118 }; 119 120 return db; 121 }]);
标签:
原文地址:http://www.cnblogs.com/weir-LV/p/5982538.html