用户
var customer = db.createObjectStore('customer',{keyPath:"id",autoIncrement: true});
customer.createIndex("customer_fName_index","fName",{unique: true});
}
}
};
// 用于处理跟回调
函数相反的方式,当defer
调用resolve
方法之后,就会触发defer.promise.then(callback)传入的callback
方法,并且resolve可以传入任意的变量
/**
*
* function test() {
* var defer = $q.defer();
* setTimeout(2000,function() {
* defer.resolve("hello");
* });
* return defer.promise;
* }
*
* test().then(function(say) {
* console.log(say);
* });
*
* 2秒之后将会打印出"hello"
*
* @type {Deferred|*}
*/
var defer = $q.defer();
_self.onload = function(cb) {
return defer.promise.then(cb);
};
var getDb = function(db) {
var d = $q.defer();
if (db) {
d.resolve(db);
}
// 打开
数据库
var result = window.indexedDB.open(myDB.name);
result.onerror = function (e) {
console.log("Open DB Error!");
d.reject("error");
};
// 正确打开
result.onsuccess = function (e) {
var db = e.target.result;
myDB.db = db;
d.resolve(db);
};
return d.promise;
};
_self.openDB = function (name,version,success,upgrade) {
var d = $q.defer();
var name = name || myDB.name;
var version = version || myDB.version;
// 打开
数据库
var result = window.indexedDB.open(name,version);
//
错误
result.onerror = function (e) {
console.log("Open DB Error!");
d.reject(e);
};
// 正确打开
result.onsuccess = function (e) {
myDB.db = e.target.result;
if (success) success(myDB.db);
d.resolve(e);
};
//
数据库版本变更
result.onupgradeneeded = function (e) {
myDB.db = e.target.result;
if (upgrade) upgrade(myDB.db);
d.resolve("upgradeneeded");
};
return d.promise;
};
var schema = function (schema) {
angular.forEach(schema,function(upgrade,o) {
_self.openDB(myDB.name,function() {
defer.resolve();
},function(db) {
upgrade(db);
});
})
};
schema(myDB.schema);
_self.get = function (storeName,key) {
var d = $q.defer(); //promise
getDb(myDB.db).then(function (db) {
var transaction = db.transaction(storeName,'readonly');
var store = transaction.objectStore(storeName);
var result = store.get(key);
result.onsuccess = function (e) {
_self.result = e.target.result;
d.resolve();
};
result.onerror = function (e) {
d.reject();
};
});
return d.promise;
};
_self.find = function (storeName,key,value) {
var d = $q.defer();//promise
getDb(myDB.db).then(function(db) {
var transaction = db.transaction(storeName,'readonly');
var store = transaction.objectStore(storeName);
var keyRange = IDBKeyRange.only(value);
var result = store.index(key).openCursor(keyRange,"next");
var results = [];
result.onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) {
results.push(cursor.value);
cursor.continue();
} else {
d.resolve(results);
}
};
result.onerror = function (e) {
d.reject();
};
});
return d.promise;
};
_self.put = function (storeName,value) {
var d = $q.defer();
var db = myDB.db || getDb();
var transaction = db.transaction(storeName,'readwrite');
var store = transaction.objectStore(storeName);
if (value !== null && value !== undefined) {
store.put(value);
d.resolve();
} else {
d.reject();
}
return d.promise;
};
_self.remove = function (storeName,value) {
var d = $q.defer();//promise
var db = myDB.db || getDb();
var transaction = db.transaction(storeName,'readwrite');
var store = transaction.objectStore(storeName);
var result = store.delete(value);
result.onsuccess = function (e) {
d.resolve();
};
result.onerror = function (e) {
d.reject();
};
return d.promise;
};
_self.findAll = function (storeName) {
var d = $q.defer();//promise
getDb(myDB.db).then(function(db) {
var transaction = db.transaction(storeName,'readonly');
var store = transaction.objectStore(storeName);
var result = store.openCursor();
var results = [];
result.onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
results.push(cursor.value);
cursor.continue();
} else {
d.resolve(results);
}
};
result.onerror = function (e) {
d.reject();
};
});
return d.promise;
};
return _self;
});
}(window,window.angular));
myUsers.js (应用的controller层脚本)
'use strict';
angular.module('myApp',['db']).controller("userCtrl",function($scope,$http,jdbc) {
// 刷新数据结构,angularjs的双向绑定会
自动帮我们渲染界面
function reload() {
jdbc.findAll("customer").then(function(response) {
if (!response) {
$http.get("data.json").success(function(response) {
$scope.users = response;
});
return;
}
$scope.users = response;
});
}
// 数据结构完成之后刷新界面
jdbc.onload(reload);
$scope.edit = true;
var _user = $scope.user = {};
$scope.editUser = function(user) {
if (user) {
_user.id = user.id;
_user.fName = user.fName;
_user.lName = user.lName;
_user.telephone = user.telephone;
} else {
_user.fName = "";
_user.lName = "";
_user.telephone = "";
_user.id = "";
}
};
$scope.deleteUser = function(id) {
// 从
数据库删除记录之后刷新表格数据
jdbc.remove("customer",id).then(reload);
};
$scope.saveCustomer = function() {
// 从
数据库添加或更新记录之后刷新表格数据
jdbc.put("customer",_user).then(reload);
};
jdbc.find("customer","customer_fName_index","林").then(function(data) {
console.log(data);
});
});
data.json(当indexedDB无法正常
获取的时候用来
显示数据用)
[
{
"id": 1,"fName": "林","lName": "嘉斌","telephone": "13514087953"
},{
"id": 2,"fName": "陈","lName": "晓","telephone": "13509890786"
}
]