PrimeFaces Ajax Callbacks

PrimeFaces partial page rendering feature is powered by JSF 2.0’s standard Ajax APIs with some of our own extensions. One of these extensions is the client side callbacks for ajax request, standard f:ajax has onerror and onevent whereas PrimeFaces provides onerror, onsuccess, onstart and oncomplete so when do these callbacks are executed?

onstart()
Before the ajax request is sent, xhr object is passed as a parameter and if you return false you can cancel the request. Also if the component that fires the ajax request supports onclick, that would be an alternative.

onerror(xhr, status, exception)
Fired when something goes wrong on server side, it gets three parameters xhr is the request object, status is the status response as string and exception is the object containing detailed information about the error.

onsuccess(xml)
Executed when server returns the partial output in xml format but before it is processed for dom update, this is handy if you’d like to parse the xml in your own. If you return false, you can cancel default dom update process.

oncomplete(xhr, status, args)
This is the most popular callback that is used, fired when response is received and dom is updated, it gets three parameters, xhr is the request object, status is the response status as string and args is the optional callback parameters that are passed from server as JSON using PrimeFaces RequestContext API. I’ll create a detailed post about how to use the handy args feature soon.

So how are these callbacks used, for example;

<p:commandButton update="component" value="Submit" oncomplete="alert('Completed')" />

An advanced demo of using these callbacks and RequestContext callback parameters is at;

http://www.primefaces.org/showcase/ui/dialogLogin.jsf

Comments are closed.

%d bloggers like this: