I build an mobile app hibrid base with phonegap and jquery mobile. My app has a login system, retrieve data from database in server and insert it to sqlite when login succes, so the app can access to the data even it's offline. i use plugin from litehelpers. And this my sql connect script in database.js:
document.addEventListener("deviceready", init, false);
//create or open Database
function connectDB () {
var db = sqlitePlugin.openDatabase("data_konfirmasi", "1.0", "Data Konfirmasi Pengiriman", "1000");
db.transaction(populateDB, errorCB, successCB);
}
//create table and insert some record
function populateDB(tx) {
tx.executeSql('DROP IF EXISTS data_konfirmasi')
tx.executeSql("CREATE TABLE IF NOT EXISTS data_konfirmasi (kode_transaksi text PRIMARY, status text DEFAULT 'Belum Terkiri'", );
}
//function will be called when an error occurred
function errorCB(err) {
alert("Error processing SQL: "+err.code);
}
//function will be called when process succeed
function successCB() {
alert("success!");
db.transaction(queryDB,errorCB);
}
connectDB;
Then, when user login for the first time and success it will retrieve data from server with json, and i want my app to insert retrieved data to sqlite. So, how to put the query for login success only? This is my login script (main.js):
$(document).on('pageinit','#login',function(){
$(document).on('click','#submit',function(){
if($('#username').val().length>0&&$('#password').val().length>0){
var un = $('#username').val();
var pw = $('#password').val();
$.ajax({
url:'http://ift.tt/1NkYmjg',
data:{ username : un,
password : pw
},
type:'post',
async:'false',
dataType: 'json',
beforeSend:function(){
$.mobile.loading('show',{theme:"a",text:"Please wait...",textonly:true,textVisible:true});
},
complete:function(){
$.mobile.loading('hide');
},
success:function(result){
console.log(result);
if(result.status==true){
user.name=result.message;
window.localStorage.setItem('konfirmasi', JSON.stringify(result.data));
// console.log('Kode: ', JSON.parse(window.localStorage['konfirmasi']));
var konfirmasi = JSON.parse(window.localStorage['konfirmasi']);
for (var i = 0; i <= konfirmasi.length; i++) {
tx.executeSql("INSERT INTO konfirmasi(kode_transaksi) VALUES (?)", konfirmasi[i]);
};
console.log('Login Berhasil');
$.mobile.changePage("#konfirmasi");
window.localStorage.setItem('uname', un);
window.localStorage.setItem('passwd', pw);
}else{
alert('Login gagal. Username atau password tidak sesuai');
}
},
error:function(request,error){
alert('Koneksi error. Silahkan coba beberapa saat lagi!');
}
});
}else{
alert('Masukkan username dan password!');
}
return false;
});
});
After that i want to make it DROP all table and localStorage when it's logout. So, i put this in main.js:
$(document).on('click', '#logout', function(e,tx){
e.preventDefault();
// log the user out
Parse.User.logOut();
window.localStorage.clear();
tx.executeSql("DROP TABLE IF EXIST data_konfirmasi",querySuccess,errorCB);
function successDrop(){
console.log('Drop table successful');
}
function errorDrop(){
console.log('Drop table unsuccessful')
}
$.mobile.changePage('#Home');
});
So, am i at the right way for logout script? I didn't know it works or not because i still cannot try it because the login script still error when i try it with insert query.
Can someone help me make it done, please?
Aucun commentaire:
Enregistrer un commentaire