Since version 1.5, jQuery has added the Deferred function to improve the event processing queue. This mechanism is used to rewrite the Ajax module. Although it is not yet Ajax's turn, this mechanism is involved in the next event handler function, so read this code in advance. Deferred registers callback functions in a queue for unified management and can call these functions synchronously or asynchronously.
JQuery. Deferred () is used to construct a Deferred object. This object has three states: Rejected, Resolved, and initial state. Resolved indicates that the operation has been completed successfully, and Rejected indicates that an error has occurred and the call has failed. The main members of the Deferred object are as follows:
Done (callback): registers a callback function and is called when the status is resolved. * Fail (callback): registers a callback function and is called when the status is rejected. * Always (callback): registers a callback function. Either resolved or rejected is called. * Then (successCallback, failureCallback): callback functions that are successful and failed at the same time. * Pipe (successFilter, failureFilter): Call the function specified by pipe before calling a successful or failed callback function. It is a pipeline mechanism that intercepts function calls. * Resolve (args): sets the status to Resolved. * Reject (args): sets the status to Rejected. * Promse (): The returned result is an incomplete Deferred interface without resolve and reject. That is, the status of the Deferred object cannot be modified. It can be viewed as a read-only view. This is to prevent external functions from triggering callback functions in advance. For example, $. ajax does not return XMLHttpRequest after version 1.5, but returns an object that encapsulates the XMLHttpRequest and Deferred object interfaces. Here, the Deferred part is obtained by promise (), so that external functions are not allowed to call resolve and reject, so as to prevent the callback function from being triggered before ajax is completed. Grant the call permissions of these two functions to ajax.
The code of this module starts from line 1, followed by the declaration of the jQuery object. It is also a basic core code. It is also one of the biggest changes in version 1.5.
In fact, the Code logic of Resolve and Reject is the same, but the corresponding status is different. For code reuse, a Deferred is implemented internally, and the real Deferred contains two new Deferred, one as Resolve, and the other as Reject.
The _ Deferred object maintains a Function Array (callback list ). The job of Done (f1, f2. ..) is to push these callback to this queue in sequence and save them. ResolveWith (resolve with parameters) and resolve call this write callback function in turn.
In Done, You need to determine whether the event has been completed. If callback has been added to the chain event, you need to execute callback immediately. This feature makes callback unnecessary and triggers asynchronous event declarations. For example, you must write $. post ("...", function (data ){...}). This success callback must be written here, but now it can be written as follows:
The Code is as follows:
Var defer = $. post ("...");
//...
Defer. success (function (data ){
//...
});
//...
Defer. fail (function (data ){
//...
});
In this way, the asynchronous event declaration and callback functions can be managed separately. This is the biggest change after version 1.5 is rewritten.
The pipe (successFilter, failureFilter) function modifies the callback list of the original object. The Filter function is inserted with the then function before the two callback lists. Then return. In this way, when the status of the Deferred object changes, the Filter function specified by the pipe function is called before callback list is called.
Promise () is much simpler, that is, a new object, and then copy the required members. The required member is defined in a constant called promiseMethods.
The Code is as follows:
Var promiseMethods = "done fail isResolved isRejected promise then always pipe". split ("");