0

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?

Ioannis Deligiannis
  • 2,679
  • 5
  • 25
  • 48

1 Answers1

1

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>)

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for the reply. These links also helped: http://stackoverflow.com/questions/7868434/are-calls-to-javascript-methods-thread-safe-or-synchronized , http://stackoverflow.com/questions/7575589/how-does-javascript-handle-ajax-responses-in-the-background?lq=1 , http://docs.oracle.com/javaee/7/javaserverfaces/2.2/jsdocs/symbols/jsf.ajax.html – Ioannis Deligiannis Dec 09 '13 at 21:11
  • Actually, although a **very bad** design, you could get hold of the `Queue` object and enqueue your requests there. BTW, the queueing mechanism for Multipart form is broken. If it queues one request, it will not process further requests. I raised the relevant bug: JAVASERVERFACES-3106 – Ioannis Deligiannis Dec 09 '13 at 22:59