XHR Filters
XHR Filter is a utility library that replaces the original $.ajax
with itself to allow manipulations on the response before the original requester gets the response.
This allows to implement things like password confirmations without needing to modify the original code in any way.
Example usage
$;
The API
$.ajaxRegisterFilter(AjaxFilter filter)
There is the method to register a new filter
AjaxFilter
This can be implemented as a class or as a plain object. All methods are optional, but obviously it is recommended to put at least one ;-)
function before(AjaxOptions settings) {}
Executed before the request is sent, you may add headers or change the options any way you want
function done(jqXHR jqXHR, Object data, AjaxOptions settings, Function reject, Function next) {}
Executed when the request was successful, you can implement any logic here, even asynchronous.
You can call reject(String reason)
if you decide the request should not pass.
function fail(jqXHR jqXHR, String thrownError, AjaxOptions settings, Function resolve, Function next) {}
Executed when the request was successful, you can implement any logic here, even asynchronous.
You can call resolve(Object data, jqXHR jqXHR)
if you decide the request should pass.
Important for
done
andfail
You must call
next()
when you're done with your logic or else the code will not know when to execute the rest of the chain of filters.The only time you can omit to call it is when you call
resolve
insidefail
orreject
insidedone