请有人建议我如何使用phonegap数据库创建角度工厂包装器。我尝试过:
angular.module('mobireward.factory', [])
.factory('dbFactory', function ($q) {
var countries=[];
var dbFactory = {};
var baseUrl = "http://182.18.157.106:8076/api/MobiRewardServices";
dbFactory.GetCountryList = function(){
//db.transaction(populateDB, errorCB, successCB);
promisedQuery('DROP TABLE IF EXISTS COUNTRY', defaultResultHandler, errorCB);
promisedQuery('CREATE TABLE IF NOT EXISTS COUNTRY (id unique, countryName, countryCode)', defaultResultHandler, errorCB);
promisedQuery('INSERT INTO COUNTRY (id, countryName, countryCode) VALUES (1, "India", "IN")', defaultResultHandler, errorCB);
promisedQuery('INSERT INTO COUNTRY (id, countryName, countryCode) VALUES (2, "Pakistan", "PK")', defaultResultHandler, errorCB);
return promisedQuery('SELECT * FROM COUNTRY', defaultResultHandler, errorCB);
};
function defaultResultHandler(deferred) {
return function(tx, results) {
var len = results.rows.length;
var output_results = [];
for (var i=0; i<len; i++){
debugger
//alert(results.rows.item(i));
output_results.push(results.rows.item(i));
}
deferred.resolve(output_results);
}
}
function defaultErrorHandler(err) {
alert("Error processing SQL: " + err.code);
}
function promisedQuery(query, successCB, errorCB) {
var deferred = $q.defer();
db.transaction(function(tx){
tx.executeSql(query, [], successCB(deferred), errorCB);
}, errorCB);
return deferred.promise;
}
// something like private methods
function errorCB(err) {
alert("Error processing SQL: " + err.code);
}
var db = window.openDatabase("Mobireward", "1.0", "Mobireward", 200000);
return dbFactory;
});