XPages JsonRPC - 更新ViewScope

时间:2017-10-10 13:47:35

标签: xpages xpages-ssjs xpages-extlib json-rpc

我无法通过按钮(onClick)从jsonRPC设置viewScope。 见下面的代码。

我正在使用浏览器(FF / Chrome Mac& PC)访问XPage。 单击按钮后,我按预期收到消息“RPC done”, 但是没有设置viewScope。

<xe:jsonRpcService id="myRpc" serviceName="myRpcService">
    <xe:this.methods>
        <xe:remoteMethod name="setDialogValues">
            <xe:this.script><![CDATA[var success = false;
try {
viewScope.put("dojoDialog_title", "Window title");
success = true;
} catch (e) {
success = false;
}
return success;]]></xe:this.script>
        </xe:remoteMethod>
    </xe:this.methods>
</xe:jsonRpcService>

<xp:button value="Set viewScope via RPC" id="setVSButton">
    <xp:eventHandler event="onclick" submit="false">
        <xp:this.script><![CDATA[var remoteMethod = myRpcService.setDialogValues();
remoteMethod.addCallback(function(response){
if(response===true){
    alert("RPC done");
}else{
    alert("ERROR");
}
});]]></xp:this.script>
    </xp:eventHandler>
</xp:button>

编辑: 我最终想要实现的是为dojo对话框设置ViewScope参数。该对话框通过CSJS打开,读取ViewScope变量并显示正确的页面,大小和标题。

我成功尝试的是在打开对话框之前使用标签设置ViewScope。我的经验告诉我,这是一个快速而肮脏的解决方案,可能随时崩溃和烧毁。当然,在运行CSJS之前必须有更好/更正的方法来设置ViewScope或运行SSJS?

请参阅下面的代码摘录。

标签 - 执行SSJS(将ViewScope设置为对话框)

<xp:label id="labelSetParamsBeforeOpeningDialog">
    <xp:this.value><![CDATA[#{javascript:
    var result = '';
try{
if(param.containsKey('runCode')){
if(param.runCode){
    var title = param.title;
    var width = param.width;
    var height = param.height;
    var dialogType = param.dialogType;

    var parentUnid = viewScope.get("currentDocUnid");

    var pageToOpen = "";
    switch(dialogType){
        case "one":
            //Dialog type specific code goes here
            pageToOpen = "dojoDialog_one.xsp?open&parent="+ parentUnid +"&dialog=true";
            break;

        case "two":
            viewScope.put("checkForConflicts", true);
            pageToOpen = 'dojoDialog_two.xsp?open&parent='+ parentUnid;
            break;

        case "three":
            pageToOpen = "dojoDialog_three.xsp?open&parent="+ parentUnid;
            break;

        default:
            pageToOpen = "error-page.xsp?open";
            break;
    }

    viewScope.put('dojoDialog_parentunid', parentUnid);
    viewScope.put('dojoDialog_pageToOpen', pageToOpen);
    viewScope.put('dojoDialog_title', title);
    viewScope.put('dojoDialog_width', width); 
    viewScope.put('dojoDialog_height', height); 
}
}

}catch (e){
    result = "ERROR:"+ e.message;
}

return result;
}]]></xp:this.value>
</xp:label>

按钮 - 运行SSJS(标签),然后打开对话框(CSJS)

<xp:button value="Show dialog three" id="myButton">
    <xp:eventHandler event="onclick" submit="false">
        <xp:this.script><![CDATA[
var title = 'THREE';
var width = 1200; 
var height = 440;
var dialogType = 'three';

XSP.partialRefreshGet("#{id:labelSetParamsBeforeOpeningDialog}",
{
    params: {"runCode":true, "title":title, "width":width, "height":height,"dialogType":dialogType},
onStart: function(){
    //nothing
},
onError: function(){
    alert("ERROR")
},
onComplete: function(){
    XSP.openDialog('#{id:inPlaceDialog}');
}
});]]></xp:this.script>
            </xp:eventHandler>
</xp:button>

Dojo对话框 - 读取ViewScope

<xe:dialog id="inPlaceDialog" style="height:auto;width:auto"
    dojoType="com.ZetaOne.widget.Dialog">
    <xe:this.title>
        <![CDATA[#{javascript:var title = 
viewScope.get("dojoDialog_title");return title;}]]>
    </xe:this.title>

    <xe:this.dojoAttributes>
        <xp:dojoAttribute name="autofocus" value="false"></xp:dojoAttribute>
    </xe:this.dojoAttributes>

    <xc:global_dojoDialog_iframe
        elementSRC="#{javascript:applicationScope.dbPath}/#
{javascript:viewScope.dojoDialog_page}"
        elementUnid="#{javascript:viewScope.dojoDialog_unid;}"
        elementParentUnid="#{javascript:viewScope.dojoDialog_parentunid;}"
        elementDialogWidth="#{javascript:viewScope.dojoDialog_width;}"
        elementDialogHeight="#{javascript:viewScope.dojoDialog_height;}">
    </xc:global_dojoDialog_iframe>

    <xp:eventHandler event="onShow" submit="false">
        <xe:this.script><![CDATA[dojo.query("img[aria-label='close 
button']").forEach(function(el){
el.src = "blank_x.gif"; 
});]]></xe:this.script>
    </xp:eventHandler>
</xe:dialog>

1 个答案:

答案 0 :(得分:1)

从我所见,JSON RPC根本不更新组件树。因此,无法从JSON RPC调用更新<iframe class="youtube" src="http://" frameborder="0" allowfullscreen></iframe>` 。见http://www.intec.co.uk/json-rpc-service-component-tree-manipulation-openlog/

如果您想更新viewScope,我不确定为什么要在部分刷新(GET或POST)上使用JSON RPC调用。