2015-04-06 48 views
0

我有一个xPages应用程序在javascript中使用全局变量。全局变量是一个NotesDocument。当xPage加载时,它会调用一些Java代码来填充全局Javascript var。全局Javascript var变为空

稍后我从该全局变量中提取一些信息,一切都很好。稍后在代码中,我尝试从试图访问该全局变量的Javascript库中运行一个函数,该变量为空。我不知道为什么。没有其他更新该全局变量。

有人可以指出我的方向来解决这个问题吗?

//This is the script library function that throws the null error: 
    var revoPayments = 
    { 
     // Calculate if the occupant should see the Revo Pay My Bill Link in the occ navigator 
     "renderPayMyBillRevo" : function() 
     { 
      var result = false; 
      try 
      { 
       // THIS IS WHERE THE GLOBAL VAR IS NULL 
       if(parseInt(gUserOccupantProfile.getShowRevoLink()) == -1) 
        result = true; 
      } catch (e) 
      { 
       print(e.message); 
      } 
      print("result: " + result); 
      return result; 
     } 
    } 
    //This is the calling javascript from the xPage which works fine pulling all the values etc. 
    if(gUserOccupantProfile && gUserOccupantProfile !== "null" && gUserOccupantProfile !== "undefined") 
    { 
     // ALL THESE VALUES ARE SET BECAUSE IT'S A VALID OBJECT 
     sessionScope.put("OccupantUNID", gUserOccupantProfile.getDocUNID()); 
     sessionScope.put("Tenant_ID", gUserOccupantProfile.gettenant_id()); 
     sessionScope.put("UnitRefNo", gUserOccupantProfile.getUnitRefNo()); 
     sessionScope.put("Resident", gUserOccupantProfile.getResident()); 
     sessionScope.put("FirstName", gUserOccupantProfile.getFirstName()); 
     sessionScope.put("LastName", gUserOccupantProfile.getLastName()); 
     sessionScope.put("CurrentUser", gUserOccupantProfile.getFirstName() + " " + gUserOccupantProfile.getLastName()); 
     sessionScope.put("PropertyAddress", gUserOccupantProfile.getPropertyAddress()); 
     var tempStr = gUserOccupantProfile.getcurrent_balance(); 
     tempStr = tempStr.replace("$", ""); 
     tempStr = tempStr.replace(",", ""); 
     sessionScope.put("Current_Balance", tempStr); 
     sessionScope.put("PropertyNo", gUserOccupantProfile.getPropertyNo()); 
     sessionScope.put("PropertyName", gUserOccupantProfile.getPropertyName()); 
     sessionScope.put("LastPaymentDate", gUserOccupantProfile.getLast_payment_date()); 
     sessionScope.put("LastPaymentAmount", gUserOccupantProfile.getPayment_amount()); 

     gAPropertyProfile = eStarService.getPropertyProfile(sessionScope.get("PropertyNo")); 
     // 09.15.2014 - Steven Rieger : added code to disable pay my bill for separate properties 
     // Default is to always display the option. 
     if(gAPropertyProfile.getDisablePayMyBill().toLowerCase() == "yes") 
      sessionScope.put("renderPayMyBill", false); 
     else 
      sessionScope.put("renderPayMyBill", true); 

     sessionScope.put("renderStatements", myEStatements.renderStatements()); 
     print("Before RenderRevo"); 
// THIS LINE FAILS BECAUSE IN THE SCRIPT LIBRARY CODE (SEE ABOVE) THE 
// GLOBAL VAR IS NULL 
     sessionScope.put("renderPayMyBillRevo", revoPayments.renderPayMyBillRevo()); 
     print("After RenderRevo"); 

    // sessionScope.put("dialogOopsTitle", "Debug!"); 
    // sessionScope.put("dialogOopsMessage", "gotLogEntry: " + gotLogEntry); 
    // var dialogOops = getComponent("dialogOops"); 
    // dialogOops.show(); 

    } 
+1

快速提示:您可能想要将'gUserOccupantProfile!==“null”&& gUserOccupantProfile!==“undefined”'更改为'gUserOccupantProfile!== null && gUserOccupantProfile!== undefined' 。 –

+0

gUserOccupantProfile是在我的脚本库顶部定义的全局(未在代码中显示) – Bitwyse1

+0

我在线上阅读了一些内容,如果它们是字符串值“null”或“undefined”,并且(gUserOccupantProfile)会捕获null或未定义的对象? – Bitwyse1

回答

4

我很确定你不能这样做序列化。您不能拥有包含带代码的方法函数的全局SSJS变量。可以包含我认为只有“静态”值的函数。 建议你看看这篇文章:http://www.notesin9.com/2014/05/20/tim-explains-ssjs-object-persistence/
并专注于Tim Tripcony的回答。

我应该补充一点,最好的解决方案是将此代码移动到Java托管bean。另一种SSJS解决方案可能不是将所有这些值放在不同的作用域变量中......创建一个HashMap并使用它来替换所有的作用域变量。那么整个地图可以传递给脚本库中的SSJS函数来检查地图并吐出你正在寻找的答案。大声想想......

+0

我会尽快审查。我不得不说,我有第二个JavaScript函数是非常相似,但行事一个不同的全球SSJS对象,这一个工作正常,所以我感到困惑。 – Bitwyse1

+0

我确实有几个bean(这两个全局变量也指向了),但是,我试图将这个逻辑从bean中删除,因为它只针对用户界面,而且bean在不同的应用程序中被全部使用。 – Bitwyse1

+1

那么你可以有一个“pageBean”充当UI的控制器。我在Jesse Gallagher的一个小框架的帮助下一直这样做 - 但是您可以手动在一个页面中手动输入。尽管我强烈建议用户界面相关代码的控制器框架工作。 –