Reputation: 6703
According to jQuery documentation, the global Ajax event handlers must be implemented using callback functions on the document. Otherwise, local $.ajax() implement events using promises.
Is there any way of handling the global Ajax events using promises method, as .done(), .fail(), .always() and .then() ?
I'm looking for a way for declaring event handlers for global ajax events that do not depend on $(document)
, for example:
// current way of doing this:
// $(document).ajaxSuccess(_handleAjaxSuccess);
//
// some ideas of what it could look like:
// $.ajaxSuccess(_handleAjaxSuccess);
// $.ajax.done(_handleAjaxSuccess);
// $.ajaxSetup({ done: _handleAjaxSuccess });
Upvotes: 0
Views: 143
Reputation: 6703
Although the promises interface is supported on jqXHR
instances, it is not supported on global Ajax events on jquery and currently there's no plan to implementing it (jquery forum).
Upvotes: 0
Reputation: 19288
No, you cannot handle the global Ajax events using promises method .done()
, .fail()
, .always()
and .then()
.
Those are Promise methods, therefore a Promise needs to exist before before they become available.
By definition, jQuery's global ajax event handlers are not attached to any particular Promise at the point where they are defined. Instead, they are stored by jQuery and invoked internally whenever certain ajax events occur.
As users of jQuery, we don't need to worry about how those internal invocations are made but we can be pretty sure that one or other of the methods .done()
, .fail()
, .always()
and .then()
is involved.
If you want to know more, you can delve into the jQuery source, however I doubt that the extra understanding would be particularly valuable.
Upvotes: 1