This forum is closed to new posts and responses. New discussions are now taking place in the IBM Developer Answers forum.



Jul 25, 2011, 8:34 PM
15 Posts

Button running server script followed by client script - is this possible?

  • Category: Server Side JavaScript
  • Platform: All
  • Release: 8.5.2
  • Role: Developer
  • Tags: SSJS,Button,javascript
  • Replies: 3
I want to do the following, and can't think of any way to do it:
 
Make a button on an xpage run a server-side JS function at the time the button is clicked, then run some client-side javascript which changes depending on the value returned from the SSJS function.
 
I know I can embed SSJS in client-side script, but that SSJS runs on page load or refresh.
The difficult thing here is that I want the SSJS to run only when the button is clicked, and have client-side script run immediately after.
 
Also, the SSJS might do a submit because it needs up-to-date field values, but it should not save the data source at that time.
 
Does anyone know how to achieve this? 
Jul 26, 2011, 8:07 AM
272 Posts
Re: Button running server script followed by client script - is this possible?
You can do this by using the "onComplete" event. Your returning value can be computed in a script block (which has to be inside a div otherwise you can not refresh the script).
 
Here is an example:
 
    <xp:button value="Label" id="button2">
        <xp:eventHandler event="onclick" submit="true"
            refreshMode="partial" refreshId="refreshMe">
            <xp:this.onComplete><![CDATA[alert(test)]]></xp:this.onComplete>
        </xp:eventHandler>
    </xp:button>

    <xp:div id="refreshMe">
        <xp:scriptBlock id="scriptBlock1">
            <xp:this.value><![CDATA[#{javascript:"var test='" + java.lang.System.currentTimeMillis() + "'";}]]></xp:this.value>
        </xp:scriptBlock>
    </xp:div>
 
 
Sven
 
Jul 29, 2011, 2:56 AM
15 Posts
Thanks, but it's actually more complicated that I initially stated...
I currently have this in the button's client-side onclick event:
 
var reqReason='#{javascript: wfRequireRejReason(compositeData.documentData)}';
if (!reqReason) return true;
dijit.byId('wfRejectionDialogue').show();
return false;
 
And this in its server-side onclick event (set to full update):
 
wfRunAction(compositeData.documentData,'WorkflowRejectNoteId',wfaTypeReject);
 
wfRunAction is a function in a SSJS library, and wfaTypeReject is a global variable (effectively a constant) in the same library.
 
Note that the client-side event includes some in-line SSJS. This is the part currently running on page load/refresh which I want to run when the button is clicked.
So... what I really want is one button click that does all this in sequence:
- Run SSJS with a full update to modify client-side JS.
- Run the modified client-side JS.
- If the client-side JS returns true, run SSJS with a full update.
 
I think I want a full update on the first piece of SSJS, because it needs to get all field values currently displayed, though I don't want data sources saved at that point.
 
I've just tried changing the client-side script to the following alternative, but it doesn't work. It seems that XSP.partialRefreshPost returns immediately (i.e. is asynchronous) and the SSJS onclick event runs without waiting for the important part of the client-side JS to finish.
 
XSP.partialRefreshPost('#{id:divRequireRejReason}',{
onComplete: function() {
alert('partialRefreshPost finished');
if (!wfReqReason) return true;
dijit.byId('wfRejectionDialogue').show();
return false;
}
});
 
Regardless of whether I get this working, your first reply is useful just because I didn't know that "onComplete" and "XSP.partialRefreshPost" existed.
Aug 2, 2011, 6:17 AM
272 Posts
Re: Button running server script followed by client script - is this possible?
Here is an example how to execute the SSJS of an onclick event from another button using the XSP.fireEvent method.
If you execute an partial refresh from the client side only and then decide to start the SSJS-Event this could resolve your problems...
Perhaps this might help you? More details on the XSP object are described here.
 
 
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
    <xp:inputText id="inputText1">
        <xp:this.value><![CDATA[#{javascript:sessionScope.get("Value");}]]></xp:this.value>
    </xp:inputText>

    <xp:button value="Label" id="button1">
        <xp:eventHandler event="onclick" submit="true"
            refreshMode="partial" refreshId="inputText1">
            <xp:this.action><![CDATA[#{javascript:var now = java.lang.System.currentTimeMillis();
            print ("now: " + now );
sessionScope.put("Value", now)}]]></xp:this.action>
        </xp:eventHandler>
    </xp:button>
    <xp:br></xp:br>
    <xp:br></xp:br>
    <xp:button value="Label" id="button2">
        <xp:eventHandler event="onclick" submit="false">
            <xp:this.script><![CDATA[XSP.fireEvent("onclick", "view:_id1:_id2", "view:_id1:button1", "onclick", function(){}, 2, "");
]]></xp:this.script>
        </xp:eventHandler>
    </xp:button>
</xp:view>
 
 

This forum is closed to new posts and responses. New discussions are now taking place in the IBM Developer Answers forum.