4

I would like to have a central place where I monitor ajax requests and in certain situations abort them.

The only thing I don't know to do is to actually abort the ajax request from one central function.

I Imagine that the solution would look something like this:

jsf.ajax.addOnEvent(function(data) { 
    if (data.status === 'begin') {
        // abort the ajax request
    }
});

But I might be mistaken.

Thanks!

Ben
  • 10,020
  • 21
  • 94
  • 157

3 Answers3

3

This is not possible by the standard JSF JS API. Your best bet is to add an onclick or onchange handler on the calling JSF component which returns false if the desired condition is met to abort the ajax request.

<h:commandButton onclick="return mayFireAjax(this)">
    <f:ajax />
</h:commandButton>

You can if necessary abstract this away with jQuery.on() (or jQuery.delegate() or jQuery.live() depending on the jQuery version used) on a common selector so that you don't need to repeat it on every desired component.

$(someSelector).on("click", function() {
    return mayFireAjax(this);
});
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

If you have control the source, you can always attach a onClick to the UICommand component. But in some situation, you do not have access to source. For example, the ajax is provided by some third-party component. You do not want to mess up with their components.

First, I have a small js library.

var FxJSFBegin = "JSFBegin";

if (jsf) {

    var originalRequest = jsf.ajax.request;

    jsf.ajax.request = function(source, oevent, options) {

        var event = $.Event(FxJSFBegin);

        event.options = options;

        event.originalEvent = oevent;

        $(source).trigger(event);

        if (event.isDefaultPrevented()) {

            return;

        } else {

            originalRequest.apply(null, arguments);

        }

    };

}

This piece code proxies original JSF ajax call. It uses jQuery to fire a "JSFBegin" event. Integration code can listen this event using jQuery mechanism. Listener can cancel the jsf call using event.preventDefault().

Requirement:

  1. jQuery
  2. This piece code should be placed after jsf.js is loaded.
Sam Hasler
  • 12,344
  • 10
  • 72
  • 106
jason zhang
  • 633
  • 6
  • 13
-1

Using global="false" will help you prevent calling the ajax status for an ajax call. Please refer to the following link.

Different Ajax statuses for different components in PrimeFaces