I use the following in one of my applications to pass a function to a custom control:
1) In the user control, add a property that will hold the function that will be executed in our action button. I will call my query querySave. The property type must be "javax.faces.el.MethodBinding". The editor should be the Method Binding Editor.
2). Here is the code behind the action button in the user control for this example:
if (compositeData.querySave) if (!compositeData.querySave.call()) return; currentDocument.save();
This says: if there is a function in the querySave property, call it. If the function returns false, do not save the document.
3) Define an SSJS function that does what you need for the action button. Usually I put mine in the SSJS library. Make sure XPage has access to this feature.
4) In the XPage that contains this control, use the editor for the property we created in step 1 (querySave in this example) and enter the name of the function created in step 3. IMPORTANT: do not add brackets or parameters when entering the function name - if you do this, the function will be executed at boot time, and not when you press the action button. Also, do not add any code directly to the editor, just the name of the function. Any code in this editor will also be executed at boot time.
Thanks to Bill Hanson for answering Expert Experts .
-
Update:
Here is a concrete example of such a custom control with a custom property, in which the SSJS function in question is called validateDocument:
<xc:component_buttons> <xc:this.validateFunctionName><![CDATA[#{javascript:validateDocument}]]></xc:this.validateFunctionName> </xc:component_buttons>
And here is an example of a button in a user control that calls a function:
<xp:button id="submit" value="Save"> <xp:eventHandler event="onclick" submit="true" refreshMode="complete"> <xp:this.action> <xp:actionGroup> <xp:this.condition><![CDATA[#{javascript: if (compositeData.validateFunctionName) { compositeData.validateFunctionName.call(); }}]]> </xp:this.condition> <xp:save></xp:save> </xp:actionGroup> </xp:this.action> </xp:eventHandler> </xp:button>