I have successfully integrated Brodysoft SQLitePlugin with angularJS for an IOS/Android app. I have 2 pages in my application.
- index.html - condition to check if the login table already exists, if not then create login table and redirect to login.html page.
- login.html - perform validation of user by executing login service.
index.html page (I have skipped the rest of script tags and the body code)
< html ng-app="app">
<head>
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
var db = window.sqlitePlugin.openDatabase({name: "DatabaseName"});
document.getElementById('dbvalue').value = db;
angular.element("#dbvalue").scope().populate(db);
}
</script>
< /head>
<body ng-controller="appCtrl">
<input type="text" id="dbvalue" style="display: none"/>
</body>
< /html>
index.js
var app = angular.module('app', []);
app.controller('appCtrl', function($scope,$http) {
$scope.populate = function(db) {
db.transaction(function(tx){
tx.executeSql("CREATE TABLE IF NOT EXISTS login_t(id int(11) NOT NULL, username varchar(50) DEFAULT NULL, password varchar(50) DEFAULT NULL, isActive int(11) NOT NULL DEFAULT '0', PRIMARY KEY (id))");
tx.executeSql("select count(*) as cnt from login_t;", [],
function(tx, res) {
if(res.rows.item(0).cnt != 0){
location.href="menu/menu.html"
}else{
location.href="login/login.html";
}
});
});
}
});
I was unable to execute var db = window.sqlitePlugin.openDatabase({name: "DatabaseName"}); directly into index.js so, As you can see in the index.html i have passed the database as a parameter to the populate(db) function which is implemented in the index.js. Everything works fine on this page. The transactions are successfully executed and the login.html page appears.
BUT,
login.html page (I have skipped the rest of script tags and the body code)
< html ng-app="login">
<head>
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
var db = window.sqlitePlugin.openDatabase({name: "DatabaseName"});
document.getElementById('dbvalue').value = db;
angular.element("#dbvalue").scope().validate(db);
}
</script>
< /head>
<body ng-controller="loginCtrl">
<input type="text" id="dbvalue" style="display: none"/>
</body>
< /html>
login.js
var loginapp = angular.module('login', []);
loginapp.controller('loginCtrl', function($scope,$http) {
$scope.validate = function(db) {
alert("Database status " + JSON.stringify(db));
db.transaction(function(tx){
alert("Begin transaction");
});
});
}
});
Here the alert message Database status appears which shows the database is successfully received till this point.
But the next line db.transaction(function(tx){ alert("Transaction begin")}); is somehow not executed and the inner alert message "Transaction begin" does not appears.
I am clueless why this is happening. No error messages on xcode7.1 console. Is their any way to rectify the issue. I hope i have provided the necessary details, looking for some expert guidence. Thanks for reading!
Aucun commentaire:
Enregistrer un commentaire