我可以使用csjs清除sessionScope变量吗?

时间:2016-06-17 17:14:58

标签: javascript xpages xpages-ssjs

我有一个很好的功能,我从一些更有经验和更好的xpages程序员偷走了使用CSJS清除sessionScope:

function clearMap( map:Map ){ // Get iterator for the keys
    var iterator = map.keySet().iterator();  // Remove all items
    while( iterator.hasNext() ){  
        map.remove( iterator.next() ); 
}

可以修改它以便从CSJS成功调用吗?

1 个答案:

答案 0 :(得分:6)

由于sessionScope是服务器端对象,因此必须使用SSJS代码清除它。您无法直接从CSJS清除它,但您可以从CSJS调用SSJS代码。要从CSJS调用SSJS,您可以使用扩展库中的JSON-RPC服务。

以下是一个例子:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" xmlns:xe="http://www.ibm.com/xsp/coreex">
    <xe:jsonRpcService id="jsonRpcService1" serviceName="myRpcService">
        <xe:this.methods>
            <xe:remoteMethod name="clearSessionScope">
                <xe:this.script>
                    <![CDATA[
                    var iterator = sessionScope.keySet().iterator();
                    while( iterator.hasNext() ){  
                        sessionScope.remove( iterator.next() );
                    }
                    return "sessionScope cleared";
                ]]>
                </xe:this.script>
            </xe:remoteMethod>
        </xe:this.methods>
    </xe:jsonRpcService>

    <xp:button value="Clear sessionScope" id="button1">
        <xp:eventHandler event="onclick" submit="false">
            <xp:this.script>
                <![CDATA[
                var deferred = myRpcService.clearSessionScope();
                deferred.addCallback(function(result) {
                  alert(result);
                });
            ]]>
            </xp:this.script>
        </xp:eventHandler>
    </xp:button>
</xp:view>