diff options
author | jaubourg <j@ubourg.net> | 2011-04-07 05:49:32 +0200 |
---|---|---|
committer | jaubourg <j@ubourg.net> | 2011-04-07 05:49:32 +0200 |
commit | bb99899ca0de93dd12f5a53f409ff6f72bfcf94c (patch) | |
tree | 5f3f1d30d288827a494a4c3a918179da8aa42bf1 /src/deferred.js | |
parent | 75a4bc4a4e55360c7c3c734cbef2558050ed4456 (diff) | |
download | jquery-bb99899ca0de93dd12f5a53f409ff6f72bfcf94c.tar.gz jquery-bb99899ca0de93dd12f5a53f409ff6f72bfcf94c.zip |
Adds always and chain methods to deferreds.
Diffstat (limited to 'src/deferred.js')
-rw-r--r-- | src/deferred.js | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/src/deferred.js b/src/deferred.js index 90f9c8089..feaccaef3 100644 --- a/src/deferred.js +++ b/src/deferred.js @@ -1,7 +1,7 @@ (function( jQuery ) { var // Promise methods - promiseMethods = "then done fail isResolved isRejected promise".split( " " ), + promiseMethods = "done fail isResolved isRejected promise then always chain".split( " " ), // Static reference to slice sliceDeferred = [].slice; @@ -100,10 +100,38 @@ jQuery.extend({ deferred.done( doneCallbacks ).fail( failCallbacks ); return this; }, + always: function() { + return deferred.done.apply( deferred, arguments ).fail.apply( this, arguments ); + }, fail: failDeferred.done, rejectWith: failDeferred.resolveWith, reject: failDeferred.resolve, isRejected: failDeferred.isResolved, + // Chain + chain: function( fnDone, fnFail ) { + return jQuery.Deferred(function( newDefer ) { + jQuery.each( { + done: [ fnDone, "resolve" ], + fail: [ fnFail, "reject" ] + }, function( handler, data ) { + var fn = data[ 0 ], + action = data[ 1 ], + returned; + if ( jQuery.isFunction( fn ) ) { + deferred[ handler ](function() { + returned = fn.apply( this, arguments ); + if ( jQuery.isFunction( returned.promise ) ) { + returned.promise().then( newDefer.resolve, newDefer.reject ); + } else { + newDefer[ action ]( returned ); + } + }); + } else { + deferred[ handler ]( newDefer[ action ] ); + } + } ); + }).promise(); + }, // Get a promise for this deferred // If obj is provided, the promise aspect is added to the object promise: function( obj ) { @@ -169,4 +197,4 @@ jQuery.extend({ } }); -})( jQuery ); +})( jQuery );
\ No newline at end of file |