Namespace jsf.ajax
The namespace for Ajax
functionality.
Defined in: jsf.js.
| Constructor Attributes | Constructor Name and Description |
|---|---|
|
The namespace for Ajax
functionality.
|
| Method Attributes | Method Name and Description |
|---|---|
| <static> |
jsf.ajax.addOnError(callback)
Register a callback for error handling.
|
| <static> |
jsf.ajax.addOnEvent(callback)
Register a callback for event handling.
|
| <static> |
jsf.ajax.request(source, event, options)
Send an asynchronous Ajax req uest to the server. |
| <static> |
jsf.ajax.response(request, context)
Receive an Ajax response from the server. |
Usage:
jsf.ajax.addOnError(handleError);
...
var handleError = function handleError(data) {
...
}
Implementation Requirements:
This function must accept a reference to an existing JavaScript function. The JavaScript function reference must be added to a list of callbacks, making it possible to register more than one callback by invokingjsf.ajax.addOnError
more than once. This function must throw an error if the callback
argument is not a function.
- Parameters:
- callback
- a reference to a function to call on an error
Usage:
jsf.ajax.addOnEvent(statusUpdate);
...
var statusUpdate = function statusUpdate(data) {
...
}
Implementation Requirements:
This function must accept a reference to an existing JavaScript function. The JavaScript function reference must be added to a list of callbacks, making it possible to register more than one callback by invokingjsf.ajax.addOnEvent
more than once. This function must throw an error if the callback
argument is not a function.
- Parameters:
- callback
- a reference to a function to call on an event
Send an asynchronous Ajax req uest to the server.
Usage:
Example showing all optional arguments:
<commandButton id="button1" value="submit"
onclick="jsf.ajax.request(this,event,
{execute:'button1',render:'status',onevent: handleEvent,onerror: handleError});return false;"/>
</commandButton/>
Implementation Requirements:
This function must:- Be used within the context of a
form. - Capture the element that triggered this Ajax request
(from the
sourceargument, also known as thesourceelement. - If the
sourceelement isnullorundefinedthrow an error. - If the
sourceargument is not astringor DOM element object, throw an error. - If the
sourceargument is astring, find the DOM element for thatstringidentifier. - If the DOM element could not be determined, throw an error.
- If the
onerrorandoneventarguments are set, they must be functions, or throw an error. - Determine the
sourceelement'sformelement. - Get the
formview state by calling jsf.getViewState passing theformelement as the argument. - Collect post data arguments for the Ajax request.
- The following name/value pairs are required post data arguments:
name value javax.faces.ViewStateContents of javax.faces.ViewState hidden field. This is included when jsf.getViewState is used.javax.faces.partial.ajaxtruejavax.faces.sourceThe identifier of the element that triggered this request.javax.faces.ClientWindowCall jsf.getClientWindow(), passing the current form. If the return is non-null, it must be set as the value of this name/value pair, otherwise, a name/value pair for client window must not be sent.
- The following name/value pairs are required post data arguments:
- Collect optional post data arguments for the Ajax request.
- Determine additional arguments (if any) from the
optionsargument. Ifoptions.executeexists:- If the keyword
@noneis present, do not create and send the post data argumentjavax.faces.partial.execute. - If the keyword
@allis present, create the post data argument with the namejavax.faces.partial.executeand the value@all. - Otherwise, there are specific identifiers that need to be sent. Create the post
data argument with the name
javax.faces.partial.executeand the value as a space delimitedstringof client identifiers.
- If the keyword
- If
options.executedoes not exist, create the post data argument with the namejavax.faces.partial.executeand the value as the identifier of the element that caused this request. - If
options.renderexists:- If the keyword
@noneis present, do not create and send the post data argumentjavax.faces.partial.render. - If the keyword
@allis present, create the post data argument with the namejavax.faces.partial.renderand the value@all. - Otherwise, there are specific identifiers that need to be sent. Create the post
data argument with the name
javax.faces.partial.renderand the value as a space delimitedstringof client identifiers.
- If the keyword
- If
options.renderdoes not exist do not create and send the post data argumentjavax.faces.partial.render. - If
options.delayexists let it be the value delay, for this discussion. Ifoptions.delaydoes not exist, or is the literal string'none', without the quotes, no delay is used. If less than delay milliseconds elapses between calls to request() only the most recent one is sent and all other requests are discarded. - If
options.resetValuesexists and its value istrue, ensure a post data argument with the namejavax.faces.partial.resetValuesand the valuetrueis sent in addition to the other post data arguments. This will causeUIViewRoot.resetValues()to be called, passing the value of the "render" attribute. Note: do not use any of the@keywords such as@formor@thiswith this option becauseUIViewRoot.resetValues()does not descend into the children of the listed components. - Determine additional arguments (if any) from the
eventargument. The following name/value pairs may be used from theeventobject:target- the ID of the element that triggered the event.captured- the ID of the element that captured the event.type- the type of event (ex: onkeypress)alt-trueif ALT key was pressed.ctrl-trueif CTRL key was pressed.shift-trueif SHIFT key was pressed.meta-trueif META key was pressed.right-trueif right mouse button was pressed.left-trueif left mouse button was pressed.keycode- the key code.
- Determine additional arguments (if any) from the
- Encode the set of post data arguments.
- Join the encoded view state with the encoded set of post data arguments
to form the
query stringthat will be sent to the server. - Create a request
contextobject and set the properties:source(the source DOM element for this request)onerror(the error handler for this request)onevent(the event handler for this request)
- Send a
beginevent following the procedure as outlined in the Chapter 13 "Sending Events" section of the spec prose document linked in the overview summary - Set the request header with the name:
Faces-Requestand the value:partial/ajax. - Determine the
posting URLas follows: If the hidden fieldjavax.faces.encodedURLis present in the submitting form, use its value as theposting URL. Otherwise, use theactionproperty of theformelement as theURL. -
Determine whether or not the submitting form is using
multipart/form-dataas itsenctypeattribute. If not, send the request as anasynchronous POSTusing theposting URLthat was determined in the previous step. Otherwise, send the request using a multi-part capable transport layer, such as a hidden inline frame. Note that using a hidden inline frame does not useXMLHttpRequest, but the request must be sent with all the parameters that a JSFXMLHttpRequestwould have been sent with. In this way, the server side processing of the request will be identical whether or the request is multipart or not.The
begin,complete, andsuccessevents must be emulated when using the multipart transport. This allows any listeners to behave uniformly regardless of the multipart orXMLHttpRequestnature of the transport.
- If the request completed successfully invoke jsf.ajax.response
passing the
requestobject. - If the request did not complete successfully, notify the client.
- Regardless of the outcome of the request (success or error) every request in the
queue must be handled. Examine the status of each request in the queue starting from
the request that has been in the queue the longest. If the status of the request is
complete(readyState 4), dequeue the request (remove it from the queue). If the request has not been sent (readyState 0), send the request. Requests that are taken off the queue and sent should not be put back on the queue.