2015-04-06 47 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的一個小框架的幫助下一直這樣做 - 但是您可以手動在一個頁面中手動輸入。儘管我強烈建議用戶界面相關代碼的控制器框架工作。 –