jQuery Ajax Chain
The $.AjaxChain jQuery helper class allows to perform multiple synchronously chained Ajax calls; its optional settings make possible to filter data and pass it between succeeding calls, manage custom errors, create caching mechanisms and conditionally halt queue progression.
Documentation
Note: although being written in plain JavaScript, this library adopts the TypeScript interface definition standard to document its features.
Constructor
name | description |
---|---|
$.AjaxChain():JQueryAjaxChain |
The $.ajaxChain helper class extends the JQueryPromise<T> object (http://api.jquery.com/Types/#Promise) with custom methods. |
Public methods
name | chainable | description |
---|---|---|
enqueue(confObj: AjaxChainConfiguration | AjaxChainConfiguration[]):JQueryAjaxChain |
yes |
Enqueues one or more configuration objects for later processing. |
dequeue():JQueryAjaxChain |
yes |
Sequentially and synchronously dequeues the configuration objects enqueued via enqueue() method in the order they were added, triggering the related Ajax calls. |
clearQueue():JQueryAjaxChain |
yes |
Clears the currently queued configuration objects. |
state():string |
no |
jQuery API Reference. |
then<U>(doneFilter: (value?: T, ...values: any[]) => U | JQueryPromise<U>, failFilter?: (...reasons: any[]) => any, progressFilter?: (...progression: any[]) => any):JQueryPromise<U> |
yes |
jQuery API Reference. |
done(doneCallback1?: JQueryPromiseCallback<T> | JQueryPromiseCallback<T>[], ...doneCallbackN: Array<JQueryPromiseCallback<T> | JQueryPromiseCallback<T>[]>):JQueryPromise<T> |
yes |
jQuery API Reference. |
fail(failCallback1?: JQueryPromiseCallback<any>|JQueryPromiseCallback<any>[], ...failCallbacksN: Array<JQueryPromiseCallback<any> | JQueryPromiseCallback<any>[]>):JQueryPromise<T> |
yes |
jQuery API Reference. |
always(alwaysCallback1?: JQueryPromiseCallback<any> | JQueryPromiseCallback<any>[], ...alwaysCallbacksN: Array<JQueryPromiseCallback<any> | JQueryPromiseCallback<any>[]>):JQueryPromise<T> |
yes |
jQuery API Reference. |
progress(progressCallback1?: JQueryPromiseCallback<any> | JQueryPromiseCallback<any>[], ...progressCallbackN: Array<JQueryPromiseCallback<any> | JQueryPromiseCallback<any>[]>):JQueryPromise<T> |
yes |
jQuery API Reference. |
Callback functions arguments
Note: differently from the original jQuery implementation, callbacks specified as arguments of then()
, done()
, fail()
, always()
methods get passed an array of responses: [response[,responseN, ...]]
. Furthermore, callbacks specified as arguments of progress()
method get passed an object structured as follows:
key | value |
---|---|
index:number |
Dequeued Ajax call zero-based index |
label:string |
Dequeued Ajax call label, either user-defined or randomly generated |
data:any |
Dequeued Ajax call response |
Configuration object
key | description |
---|---|
ajaxSettings:JQueryAjaxSettings |
jQuery $.ajax method settings. |
label?:string |
Configuration object label (see progress() callback functions argument). |
transform?:Function |
Returning a truthy value allows to arbitrarily overwrite the next queued Ajax call 'data' property value specified in the original jQuery $.ajax method configuration object ('ajaxSettings'). See example #1. |
appendToUrl?:Function |
Returning a truthy value (String) allows to append string fragments to the next queued Ajax call 'url' property value specified in original jQuery $.ajax method configuration object ('ajaxSettings'); handy when dealing with semantic urls. See example #2. |
hasErrors?:Function |
Returning a truthy value determines any registered fail callback(s) to be called immediately, passing the former as an argument; the queue is then rejected. See example #3. |
hasCache?:Function |
Returning a truthy value allows to prevent the related Ajax call from being executed, passing the former as a parameter to any registered handler(s); useful to create caching mechanisms. See example #4. |
hasHaltingCapabilities?:Function |
Returning a truthy value prevents the queue from further progressing to the succeeding Ajax calls; the queue is then resolved. See example #5. |
isSkippable?:Function |
Returning a truthy value prevents the queue from being halted in case of $.Ajax error. See example #6. |
Note: All functions specified as values of optional configuration object properties (marked with a question mark) are passed the dequeued Ajax call response as argument.
Examples
Typical setup
var ajaxChain = ;ajaxChain ;
Common configuration patterns
<!-- The succeeding examples assume the following xml response: --> Item #1 1 Item #2 2 Item #3 3
1. Dynamically passing data between succeeding Ajax calls:
var ajaxChain configurationObj1 configurationObj2;ajaxChain = ;configurationObj1 = ajaxSettings: type: "GET" dataType: "xml" url: "/items" { var nextCallDataObj; if xmlResponse nextCallDataObj = id: ; return nextCallDataObj; return false; };// configuration object omitted for brevityconfigurationObj2 = ;ajaxChain ;
2. Dynamically appending url fragments between succeeding Ajax calls:
var ajaxChain configurationObj1 configurationObj2;ajaxChain = ;configurationObj1 = ajaxSettings: type: "GET" dataType: "xml" url: "/items" { var nextCallUrlFragment = ""; if xmlResponse nextCallUrlFragment = "/" + ; ; return nextCallUrlFragment; } ;// configuration object omitted for brevityconfigurationObj2 = ;ajaxChain ;
3. Dynamically resolving queue upon custom errors:
var ajaxChain configurationObj1 configurationObj2;ajaxChain = ;configurationObj1 = ajaxSettings: type: "GET" dataType: "xml" url: "/items" { var $tempXmlResponse categoryFilter = "1"; $tempXmlResponse = ; // check current Ajax call response for errors if $tempXmlResponse text !== -1 return "[Exception] forbidden category id: " + categoryFilter; return false; };// configuration object omitted for brevityconfigurationObj2 = ;ajaxChain ;
4. Creating caching mechanisms:
var ajaxChain configurationObj1 configurationObj2;ajaxChain = ;configurationObj1 = ajaxSettings: type: "GET" dataType: "xml" url: "/items" { // retrieve possible cached results if itemsCollectionlength return itemsCollection; return false; };// configuration object omitted for brevityconfigurationObj2 = ;ajaxChain ;
5. Halting queue upon conditional logic:
var ajaxChain configurationObj1 configurationObj2;ajaxChain = ;configurationObj1 = ajaxSettings: type: "GET" dataType: "xml" url: "/items" { var $tempXmlResponse; $tempXmlResponse = ; if $tempXmlResponse text !== -1 return true; return false; };// configuration object omitted for brevityconfigurationObj2 = ;ajaxChain ;
6. Preventing a queue from being halted in case of $.Ajax error:
var ajaxChain configurationObj1 configurationObj2;ajaxChain = ;configurationObj1 = ajaxSettings: type: "GET" dataType: "xml" url: "/items" { return true; };// configuration object omitted for brevityconfigurationObj2 = ;ajaxChain ;
TypeScript
Visual Studio (NuGet)
- Open the Package Manager Console
Install-Package jquery-ajax-chain.TypeScript.DefinitelyTyped
Node.js (TSD)
- Open a shell in your project's scripts folder
npm install tsd -g
tsd install jquery-ajax-chain --save
Testing
Headless
- Open a shell in the package root folder
npm install
bower install
grunt test:headless
Browser
- Open a shell in the package root folder
npm install
bower install
grunt test:browser
(--port
option available, e.g.:grunt test:browser --port=8083
)
Note: this project was packaged with grunt-init-jquery.