简体   繁体   English

Worklight-jQuery移动版| 如何检查用户是否在pagebeforeshow中登录?

[英]Worklight - jquery mobile | How to check if a user is logged in in pagebeforeshow?

I am trying to check if a user is set (global variable is not undefined/null) before every page is loaded in jquery mobile. 我正在尝试在jquery mobile中加载每个页面之前检查是否已设置用户(全局变量不是未定义/空)。

What is the best way to do this? 做这个的最好方式是什么?

I thought to do this 我想这样做

$(document).on("pagebeforeshow", "#mainPage", function(){
if(window.klappr.userId === undefined || window.klappr.userId == null){
                  $.mobile.changePage($("#loginPage"));
        }else{
loadMainOverview();
         }
});

But how to stop the first changePage? 但是如何停止第一个changePage? (to main) Isn't there a better way to check this for all the protected pages? (主要)没有更好的方法来检查所有受保护页面的方法吗?

Use pagebeforechange , as it will prevent showing the target page. 使用pagebeforechange ,因为它将阻止显示目标页面。 Using pagebeforeshow will result in showing the target page and then jump to #loginpage. 使用pagebeforeshow将导致显示目标页面,然后跳转到#loginpage。

$(document).on("pagebeforechange", function(e) {
  if(window.klappr.userId === undefined || window.klappr.userId == null) {
    $.mobile.changePage($("#loginPage"));
    e.preventDefault();
  } 
  else {
    loadMainOverview();
  }
});

jquery mobile provide local storage store data in local storage, you can use as globally. jQuery mobile提供本地存储将数据存储在本地存储中,您可以将其用作全局存储。

 localStorage.setItem("userId", value);
 localStorage.getItem(userId);

 $(document).on("pagebeforeshow", "#mainPage", function(){
 if(localStorage.getItem(userId) === undefined || localStorage.getItem(userId) == null){
              $.mobile.changePage($("#loginPage"));
    }else{
 loadMainOverview();
     }
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM