+
+ // Create a simple deferred (one callbacks list)
+ _deferred: function( cancellable ) {
+
+ // cancellable by default
+ cancellable = cancellable !== false;
+
+ var // callbacks list
+ callbacks = [],
+ // stored [ context , args ]
+ fired,
+ // to avoid firing when already doing so
+ firing,
+ // flag to know if the deferred has been cancelled
+ cancelled,
+ // the deferred itself
+ deferred = {
+
+ // then( f1, f2, ...)
+ then: function () {
+
+ if ( ! cancelled ) {
+
+ var args = arguments,
+ i,
+ length,
+ elem,
+ type,
+ _fired;
+
+ if ( fired ) {
+ _fired = fired;
+ fired = 0;
+ }
+
+ for ( i = 0, length = args.length ; i < length ; i++ ) {
+ elem = args[ i ];
+ type = jQuery.type( elem );
+ if ( type === "array" ) {
+ deferred.then.apply( deferred , elem );
+ } else if ( type === "function" ) {
+ callbacks.push( elem );
+ }
+ }
+
+ if ( _fired ) {
+ deferred.fire( _fired[ 0 ] , _fired[ 1 ] );
+ }
+ }
+ return this;
+ },
+
+ // resolve with given context and args
+ // (i is used internally)
+ fire: function( context , args , i ) {
+ if ( ! cancelled && ! fired && ! firing ) {
+ firing = 1;
+ try {
+ for( i = 0 ; ! cancelled && callbacks[ i ] ; i++ ) {
+ cancelled = ( callbacks[ i ].apply( context , args ) === false ) && cancellable;
+ }
+ } catch( e ) {
+ cancelled = cancellable;
+ jQuery.error( e );
+ } finally {
+ fired = [ context , args ];
+ callbacks = cancelled ? [] : callbacks.slice( i + 1 );
+ firing = 0;
+ }
+ }
+ return this;
+ },
+
+ // resolve with this as context and given arguments
+ resolve: function() {
+ deferred.fire( this , arguments );
+ return this;
+ },
+
+ // cancelling further callbacks
+ cancel: function() {
+ if ( cancellable ) {
+ callbacks = [];
+ cancelled = 1;
+ }
+ return this;