我试图在加载我的应用程序时触发和事件:当应用程序启动时,我加载#主页,但如果用户没有被缓存(我使用kinvey),应用程序应该将页面更改为#login。我如何才能做到这一点?
我在myScript.js文件的开头尝试了这段代码,这是在开头声明的:
$('#home').on( 'pageshow',function(){
var user = Kinvey.getActiveUser();
var promise = Kinvey.User.me({
success: function(response) {
$.mobile.changePage('#login'); //should change page here
});
}我很感激关于触发什么事件的建议,因为jquerymobile文档中有很多,我不知道哪一个更方便。
发布于 2014-05-17 02:57:27
尝试:
$(document).on( 'mobileinit' , function(){ // will fire as soon as jQM is loaded
var user = Kinvey.getActiveUser();
var promise = Kinvey.User.me({
success: function(response) {
$.mobile.changePage('#login', {transition: 'none'}); //should change page here
});
}替代方案可能包括:
$('#home').on( 'pageinit' ,function(){ /*stuff*/ }); //will fire the first time #home is loaded
$('#home').on( 'pagebeforeshow' ,function(){ /*stuff*/ }); //will fire before home is shown (don't do this, it'll check every time)如果你使用的是cordova,并且在config中启用了启动页面,你可以尝试一下:
$(document).on( 'mobileinit' , function(){ // will fire as soon as jQM is loaded
var user = Kinvey.getActiveUser();
var promise = Kinvey.User.me({
success: function(response) {
$.mobile.changePage('#login', {transition: 'none'}); //should change page here
if (navigator.splashscreen /* || 1 */ ){ // so it doesn't crash when you're testing on your browser
navigator.splashscreen.hide();
}
}
});https://stackoverflow.com/questions/23698464
复制相似问题