Javascript jQuery mobile pageshow事件未在第一个$.mobile.changePage()上触发
我正在写一个phonegap/jquery移动应用程序,有一个问题我似乎无法解决 当应用程序加载设备就绪并启动jqm_mobile_init时,应用程序会创建/打开一个数据库,并检查用户是否仅登录数据库中的一个标志。如果是这样,应用程序将调用$.mobile.changePagehome,{transition:none};将它们重定向到主页 然后在HomePage pageshow事件中,我从数据库中获取一个信息负载,并将其附加到主页内的listview中 但是,第一次使用$.mobile.changePage事件运行时,不会触发pageshow事件,因此我的任何数据都不会附加到listview。如果我浏览应用程序,然后访问页面,数据显示良好。这仅在使用$.mobile.changePage更改到主页时发生 如何在$.mobile.changePage上设置pageshow fire?还是有别的办法 这是我的密码:Javascript jQuery mobile pageshow事件未在第一个$.mobile.changePage()上触发,javascript,cordova,jquery-mobile,Javascript,Cordova,Jquery Mobile,我正在写一个phonegap/jquery移动应用程序,有一个问题我似乎无法解决 当应用程序加载设备就绪并启动jqm_mobile_init时,应用程序会创建/打开一个数据库,并检查用户是否仅登录数据库中的一个标志。如果是这样,应用程序将调用$.mobile.changePagehome,{transition:none};将它们重定向到主页 然后在HomePage pageshow事件中,我从数据库中获取一个信息负载,并将其附加到主页内的listview中 但是,第一次使用$.mobile.c
/************************************************
Try to create/open the DB, if not catch the error
***********************************************/
try {
if (!window.openDatabase) {
alert('not supported');
} else {
var shortName = 'test';
var version = '1.0';
var displayName = 'test Database';
var maxSize = 200000; // in bytes
// database instance in db.
var db = openDatabase(shortName, version, displayName, maxSize);
// Create tables
createTables(db);
// Check if there is a signedin user
isUserSignedInQuery(db);
}
} catch(e) {
// Error handling code goes here.
if (e == 2) {
// Version number mismatch.
alert("Invalid database version.");
} else {
alert("Unknown error "+e+".");
}
return;
}
// Universal null/blank data handler
function nullDataHandler(transaction, results) { }
// Universal error callback
function errorHandler(error) {
//alert("Error processing SQL: " +error.message+ " Error Code: " +error.code);
}
// Create tables if dont already exist
function createTables(db) {
db.transaction(
function (transaction) {
// create tables
}
);
}
/**********************************************************************************************
Check if there is a signed in user, if so redirect to listings page, if not display login page
**********************************************************************************************/
function isUserSignedInQuery(db) {
db.transaction(
function (transaction) {
transaction.executeSql("SELECT * FROM USERS WHERE signedIn=1;",
[], // array of values for the ? placeholders
isUserSignedInDataHandler, errorHandler);
}
);
}
function isUserSignedInDataHandler(transaction, results) {
// Handle the results
if (results.rows.length > 0) {
//console.log("someones logged in!");
// Assign signed in user to global var
console.log("logged in user = " + results.rows.item(0).id);
window.currentSignedInUserId = results.rows.item(0).id;
$.mobile.changePage( "#home", { transition: "none"} );
} else {
$.mobile.changePage( "#login", { transition: "none"} );
}
}
/**********************************************************************************************
Sign in page:
**********************************************************************************************/
function doesSigningInUserAlreadyExistQuery(db) {
db.transaction(
function (transaction) {
transaction.executeSql("SELECT * FROM USERS WHERE username='"+usernameValue+"' ORDER BY id LIMIT 0,1;",
[], // array of values for the ? placeholders
doesSigningInUserAlreadyExistDataHandler, errorHandler);
}
);
}
function doesSigningInUserAlreadyExistDataHandler(transaction, results) {
// User exists, sign them in.
if (results.rows.length > 0) {
//console.log("user exists");
// Find number of rows
var len = results.rows.length;
//console.log(len);
for (var i=0; i<len; i++){
//console.log(results.rows.item(i));
db.transaction(
function (transaction) {
transaction.executeSql('UPDATE USERS SET signedIn = 1 WHERE username="'+usernameValue+'"');
}
);
// Assign signed in user to global var
window.currentSignedInUserId = results.rows.item(0).id;
// Redirect to home/listings page
$.mobile.changePage( "#home", { transition: "slidefade"} );
}
// User is new, create them and sign them in
} else {
db.transaction(
function (transaction) {
transaction.executeSql('INSERT INTO USERS (username, password, userId, defaultHandler, autoSync, updateCaseTypes'
+', updateHistorical, updateFavorite, signedIn) '
+'VALUES ("'+usernameValue+'", "eclipse", "userid321", "Another User", 1, 1, 1, 1, 1);', [],
function (transaction, resultSet) {
if (!resultSet.rowsAffected) {
// Previous insert failed.
alert('No rows affected!');
return false;
}
alert('insert ID was '+resultSet.insertId);
//Assign signed in user to global var
window.currentSignedInUserId = resultSet.insertId;
});
}
);
// Redirect to home/listings page
$.mobile.changePage( "#home", {
reloadPage: true,
transition: "slidefade"} );
}
}
$('#login').live('pageshow', function(event) {
console.log(window.currentSignedInUserId); // This is empty - global var not working
// Should this be tap??????? Find out. -----------
$('a#signIn').click(function() {
// Get values of all fields & buld vars
var username = $('#login-username');
var password = $('#login-password');
// Check if fields are empty
if( !username.val() ) {
username.addClass('empty');
$('label.login-username').addClass('blank');
}
if( !password.val() ) {
password.addClass('empty');
$('label.login-password').addClass('blank');
}
// If both not empty, check if user exists, if so sql update if not sql insert
if (username.val() && password.val()) {
// Get username
usernameValue = username.val();
// Run function
doesSigningInUserAlreadyExistQuery(db);
}
});
});
$('#home').live('pageshow', function(event) {
console.log("Page show fired on recordings page");
db.transaction(getRecordingsQuery, getRecordingsDataHandler, errorHandler);
// get stuff, loop through it and append
// Refresh the list to add JQM styles etc
$('#recordings-list').listview('refresh');
}
});
你应该在电视上使用而不是直播。live已被弃用。
你试过把它放在beforepageshow而不是pageshow上吗?它似乎是放置数据收集/动态页面元素生成的更好地方。我已经设法解决了它,这不是一个真正正确的修复方法,但它的工作是以屏幕刷新时屏幕闪烁为代价的
如果它对任何人都有帮助,我添加了allowSamePageTransitions:true,它以牺牲闪烁为代价解决了问题。感谢您的回复,我尝试了“无差别”和“无差别”显示“无差别”唯一似乎可以让它工作但不理想的方法是强制加载刷新:$.mobile.changePage home,{reloadPage:true,这并不理想,因为页面会明显刷新-还有其他想法吗?尝试$document。在'pageshow','home',函数{…};上。不,当我四处导航并通过链接访问它时,这完全不起作用。这可能与作用域有关吗?上面的代码包装在这个var startApp=函数中{