6.8.  < a4j:jsFunction >

6.8.1. Description

The <a4j:jsFunction> component allows to invoke the server side data and return it in a JSON format to use in a client JavaScript calls.

Table 6.15. a4j : jsFunction attributes

Attribute NameDescription
actionMethodBinding pointing at the application action to be invoked, if this UIComponent is activated by you, during the Apply Request Values or Invoke Application phase of the request processing lifecycle, depending on the value of the immediate property
actionListenerMethodBinding pointing at method accepting an ActionEvent with return type void
ajaxSingleif "true", submits ONLY one field/link, instead of all form controls
bindingThe attribute takes a value-binding expression for a component property of a backing bean
bypassUpdatesIf "true", after process validations phase it skips updates of model beans on a force render response. It can be used for validating components input
dataSerialized (on default with JSON) data passed on the client by a developer on AJAX request. It's accessible via "data.foo" syntax
eventsQueueName of requests queue to avoid send next request before complete other from same event. Can be used to reduce number of requests of frequently events (key press, mouse move etc.)
focusid of element to set focus after request completed on client side
idEvery component may have a unique id that is automatically created if omitted
ignoreDupResponsesAttribute allows to ignore an Ajax Response produced by a request if the newest 'similar' request is in a queue already. ignoreDupResponses="true" does not cancel the request while it is processed on the server, but just allows to avoid unnecessary updates on the client side if the response isn't actual now
immediateTrue means, that the default ActionListener should be executed immediately (i.e. during Apply Request Values phase of the request processing lifecycle), rather than waiting until the Invoke Application phase
limitToListIf "true", updates on client side ONLY elements from this 'reRender' property. If "false" (default) updates all rendered by ajax region components
nameName of generated JavaScript function definition
onbeforedomupdateJavaScript code for call before DOM has been updated on client side
oncompleteJavaScript code for call after request completed on client side
processId['s] (in format of call UIComponent.findComponent()) of components, processed at the phases 2-5 in case of AjaxRequest caused by this component. Can be single id, comma-separated list of Id's, or EL Expression with array or Collection
renderedIf "false", this component is not rendered
requestDelayAttribute defines the time (in ms.) that the request will be wait in the queue before it is ready to send. When the delay time is over, the request will be sent to the server or removed if the newest 'similar' request is in a queue already
reRenderId['s] (in format of call UIComponent.findComponent()) of components, rendered in case of AjaxRequest caused by this component. Can be single id, comma-separated list of Id's, or EL Expression with array or Collection
statusID (in format of call UIComponent.findComponent()) of Request status component
timeoutResponse waiting time on a particular request. If a response is not received during this time, the request is aborted

Table 6.16. Component identification parameters

NameValue
component-typeorg.ajax4jsf.Function
component-familyorg.ajax4jsf.components.ajaxFunction
component-classorg.ajax4jsf.component.html.HtmlajaxFunction
renderer-typeorg.ajax4jsf.components.ajaxFunctionRenderer

6.8.2. Creating on a page

Simple component definition example:

Example:


...
<head>
    <script>
        <!--There is some script named "myScript" that uses parameters which will be taken from server-->
    </script>
</head>
<body>
...
<a4j:jsFunction data="#{bean.someProperty}" name="callScript" oncomplete="myScript(data.subProperty1, data.subProperty2)"/>
...
    

The script "myScript" is called after bean.someProperty data is returned from server(e.g. It'll be object with two subproperties).

6.8.3. Creating the Component Dynamically Using Java

Example:


import org.ajax4jsf.component.html.HtmlajaxFunction;
...
HtmlajaxFunction myFunction = new HtmlajaxFunction();
...

6.8.4. Key attributes and ways of usage

As the component uses Ajax request to get data from server - it has all common Ajax Action attributes. Hence, "action" and "actionListener" can be invoked, and reRendering some parts of the page fired after calling function.

When using the <a4j:jsFunction> it's possible to initiate the Ajax request from the JavaScript and perform partial update of a page and/or invoke the JavaScript function with data returned by Ajax response.


...
<body onload="callScript()">
    ...
    <h:form>
    ...
    <a4j:jsFunction name="callScript" data="#{bean.someProperty1}"
        reRender="someComponent" oncomplete="myScript(data.subProperty1, data.subProperty2)">
        <a4j:actionParam name="param_name" assignTo="#{bean.someProperty2}"/>
    </a4j:jsFunction>
    ...
    </h:form>
    ...
</body>
...

The <a4j:jsFunction> allows to use <a4j:actionParam> or pure <f:param> for passing any number of parameters of the JavaScript function into Ajax request. <a4j:jsFunction> is similar to <a4j:commandButton> , but it could be activated from the JavaScript code. It allows to invoke some server side functionality and use the returned data in the JavaScript function invoked from "oncomplete" attribute. Hence it's possible to use <a4j:jsFunction> instead of <a4j:commandButton> . You can put it anywhere, just don't forget to use <h:form> ... </h:form> around it.

Information about the "process" attribute usage you can find here.

6.8.5. Relevant resources links

Here you can see the example of <a4j:jsFunction> usage and sources for the given example.

Here you can found some additional information about <f:param> component.