全局Javascript var变为null

时间:2015-04-06 16:40:41

标签: javascript xpages

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

稍后我从全局var中提取了一些信息,一切都很好。稍后在代码中我从Javascript库运行一个函数,该函数试图访问该全局var并且它为null。我不确定为什么。没有其他更新全局变量。

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

    //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 个答案:

答案 0 :(得分:4)

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

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