By default JSF ajax(JS) events are queued so that they are executed in order. Is there a way to hook into that framework?
What I am trying to do, is to get my JS events to use this queue as well. Is the global variable that I can use?
By default JSF ajax(JS) events are queued so that they are executed in order. Is there a way to hook into that framework?
What I am trying to do, is to get my JS events to use this queue as well. Is the global variable that I can use?
There isn't, but you can hook an event listener on JSF ajax request(s) via <f:ajax onevent>
and jsf.ajax.addOnEvent
. You can then execute some JS code before the ajax request is sent, or after the ajax response is arrived, and/or after the HTML DOM is updated. Depending on the concrete functional requirement, that may be actually what you're looking for.
Here's how such an event listener function can look like:
function someAjaxEventListener(data) {
var status = data.status;
switch(status) {
case "begin":
// This is invoked right before ajax request is sent.
break;
case "complete":
// This is invoked right after ajax response is arrived.
break;
case "success":
// This is invoked right after HTML DOM is updated.
break;
}
}
And here's how you can register it on a per-<f:ajax>
basis:
<f:ajax ... onevent="someAjaxEventListener" />
And here's how you can register it on a view-wide basis (thus, hooking on every <f:ajax>
):
<h:outputScript>jsf.ajax.addOnEvent(someAjaxEventListener);</h:outputScript>
(preferably put it in some JS file which you load by <h:outputScript name>
)