【问题标题】:Best way to fire an event when app loads a page in cordova app singlepage with jquery mobile?当应用程序使用 jquery mobile 在科尔多瓦应用程序单页中加载页面时触发事件的最佳方法?
【发布时间】:2014-05-16 14:57:50
【问题描述】:

我正在尝试在加载我的应用程序时触发和事件: 当应用程序启动时,我会加载#home 页面,但如果用户没有被缓存(我使用 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 文档中有很多,我不知道哪个更方便。

【问题讨论】:

  • 您在等待 Cordova(Phonegap) 或 jQuery Mobile 加载吗?
  • 我想在应用初始化时进行检查。我应该等待 jQueryMobile 和 kinvey init,这样我才能决定加载哪个页面。
  • 看看我的回答,告诉我它是否有效。

标签: javascript jquery-mobile cordova kinvey jquery-mobile-pageshow


【解决方案1】:

尝试:

$(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 并在配置中启用了启动页面,您可以尝试:

$(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(); 
        }
    }
});

【讨论】:

  • 谢谢,但以上都不起作用。 kinvey 需要一段时间才能连接,所以我总是得到“kinvey 未定义”。现在我正在尝试通过本地存储跟踪用户访问,它应该更快更安全,因为它不需要连接,我只需要加载 Cordova。
  • 我解决了这个问题...我做了第一页#login,在我的登录函数中,我在成功回调中添加了一个存储用户名的请求,以及一个检查该值的存在。一切,工作正常。如果我按下手机上的后退按钮,我会退出应用程序,这是应该的。无论如何,感谢您的宝贵时间。
猜你喜欢
  • 1970-01-01
  • 2014-07-08
  • 2017-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多