SSJS code is not executed in XPages onclick

194 Views Asked by At

In my code I have the following Event Handler

<xp:eventHandler event="onclick" submit="true"
                        refreshMode="partial" disableValidators="true"
                        refreshId="#{javascript:compositeData.refreshid}">

                            <xp:this.script><![CDATA[
                            alert('clicked');
                            ]]></xp:this.script>

                        <xp:this.action><![CDATA[#{javascript:print("jkafkjsfjk");}]]></xp:this.action>
</xp:eventHandler>

Unfortunately, I never get jkafkjsfjk printed in the console while CSJS code works fine and there's clicked alert pops up.

I know that the most common reason of not executing it has to do with some required fields equal to null or empty.

However, I've commented everything out but the code isn't executed anyway..

How do I resolve that and what's the reason?

Thanks in advance.

1

There are 1 best solutions below

2
John Dalsgaard On

A couple of things...

First, if your CSJS returns false then the SSJS never runs (which is handy when you want the user to confirm...). I guess you should be alright here - but you could return true to be safe....

Second, partial refresh's, refresh ids, and partial executions can do things that may not be what you expect. So for the test I would just do a complete refresh and skip the id.

What kind of object is the eventhandler set on?

I normally use these kind of event handlers for links or buttons like:

<xp:button value="Add zone" id="button2" styleClass="btn-xs btn-primary">
    <xp:eventHandler event="onclick" submit="true" refreshMode="partial" refreshId="fields">
        <xp:this.action><![CDATA[#{javascript:AssociationAdmin.createZone()}]]></xp:this.action>
    </xp:eventHandler>
</xp:button>

And this works fine :-)

Here is an example with a confirmation first:

<xp:link escape="true" text="Delete" id="link5" styleClass="action" rendered="#{javascript:!AssociationAdmin.isCatchInfoInUse(row.getKey())}">
    <xp:eventHandler event="onclick" submit="true" refreshMode="complete" id="eventHandler5">
        <xp:this.action><![CDATA[#{javascript:AssociationAdmin.removeCatchInfo(row.key);}]]></xp:this.action>
        <xp:this.script><![CDATA[return window.confirm("Are you sure you want to delete these fields: #{javascript:row.getName()}?\n\nDeletion cannot be undone!!")]]></xp:this.script>
    </xp:eventHandler>
</xp:link>

Further notes:

You may want to submit without validating. You can do this by adding disableValidators="true" to your eventhandler

Please remember to have an id on the control where you have your eventhandler. Just wasted an evening and morning with the most strange behaviour cased by the button opening dialog did NOT have and id attributee...

Hope these samples will get you going :-)

/John