Thorben Croisé
Thorben Croisé

Reputation: 12870

Execute Javascript *after* ajax update

I need to execute a certain javascript function after the ajax update has been applied. I tried jsf.ajax.addOnEvent(ch.ergon.newom.ajaxUpdate) to get informed about the request, but that seems to be execute after the ajax response arrived, but before it got applied (that is: the content got updated).

How can I execute a JavaScript function after the content got updated by JSF-ajax?

Upvotes: 1

Views: 1898

Answers (1)

BalusC
BalusC

Reputation: 1108722

Actually, this will be called three times. One before the ajax request is sent, one after the ajax response is arrived and one when the ajax response is successfully processed. You need to check the current status based on the provided data argument. You can find technical details in tables 14-4 and 14-3 of the JSF 2.0 specification.

Here's a kickoff example of how your JS function should look like when you'd like to hook on all 3 of the statuses.

function ajaxUpdate(data) {
    var ajaxStatus = data.status; // Can be 'begin', 'complete' and 'success'.

    switch (ajaxStatus) {
        case 'begin': // This is called right before ajax request is been sent.
            // ...
            break;

        case 'complete': // This is called right after ajax response is received.
            // ...
            break;

        case 'success': // This is called when ajax response is successfully processed.
            // ...
            break;
    }
}

Or when you'd like to hook on the success status only:

function ajaxUpdate(data) {
    if (data.status == 'success') {
        // ...
    }
}

Upvotes: 2

Related Questions