2016-06-17 51 views
1

我有一個很好的功能,我從一些更有經驗和更好的XPages編程器,它會清除sessionScope使用CSJS偷:我可以使用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成功叫?

回答

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> 
+0

謝謝 - 我會給本週稍後再試。 –

相關問題