I am having troubles setting viewScope from jsonRPC via button (onClick). See code below.
I am using a browser (FF/Chrome Mac & PC) to access the XPage. After clicking the button i get message "RPC done" as expected, but the viewScope is not set.
<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>
EDIT: What I ultimately want to achieve is setting ViewScope parameters for a dojo dialog. The dialog is opened via CSJS, reads the ViewScope variables and displays the correct page, size and title.
What I have tried successfully is using a label to set ViewScope before opening the dialog. My experience tells me this is a quick and dirty solution that might crash and burn at any time. Surely there must be a better / correct way to set ViewScope or run SSJS before running CSJS?
See code excerpt below.
Label - Execute SSJS (Set ViewScope for dialog)
<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>
Button - Run SSJS (label) then open dialog (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 dialog - Reads 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>