|
define( [ |
|
"./core", |
|
"./var/slice", |
|
"./callbacks" |
|
], function( jQuery, slice ) { |
|
|
|
jQuery.extend( { |
|
|
|
Deferred: function( func ) { |
|
var tuples = [ |
|
|
|
|
|
[ "resolve", "done", jQuery.Callbacks( "once memory" ), "resolved" ], |
|
[ "reject", "fail", jQuery.Callbacks( "once memory" ), "rejected" ], |
|
[ "notify", "progress", jQuery.Callbacks( "memory" ) ] |
|
], |
|
state = "pending", |
|
promise = { |
|
state: function() { |
|
return state; |
|
}, |
|
always: function() { |
|
deferred.done( arguments ).fail( arguments ); |
|
return this; |
|
}, |
|
then: function( ) { |
|
var fns = arguments; |
|
return jQuery.Deferred( function( newDefer ) { |
|
jQuery.each( tuples, function( i, tuple ) { |
|
var fn = jQuery.isFunction( fns[ i ] ) && fns[ i ]; |
|
|
|
|
|
deferred[ tuple[ 1 ] ]( function() { |
|
var returned = fn && fn.apply( this, arguments ); |
|
if ( returned && jQuery.isFunction( returned.promise ) ) { |
|
returned.promise() |
|
.progress( newDefer.notify ) |
|
.done( newDefer.resolve ) |
|
.fail( newDefer.reject ); |
|
} else { |
|
newDefer[ tuple[ 0 ] + "With" ]( |
|
this === promise ? newDefer.promise() : this, |
|
fn ? [ returned ] : arguments |
|
); |
|
} |
|
} ); |
|
} ); |
|
fns = null; |
|
} ).promise(); |
|
}, |
|
|
|
|
|
|
|
promise: function( obj ) { |
|
return obj != null ? jQuery.extend( obj, promise ) : promise; |
|
} |
|
}, |
|
deferred = {}; |
|
|
|
|
|
promise.pipe = promise.then; |
|
|
|
|
|
jQuery.each( tuples, function( i, tuple ) { |
|
var list = tuple[ 2 ], |
|
stateString = tuple[ 3 ]; |
|
|
|
|
|
promise[ tuple[ 1 ] ] = list.add; |
|
|
|
|
|
if ( stateString ) { |
|
list.add( function() { |
|
|
|
|
|
state = stateString; |
|
|
|
|
|
}, tuples[ i ^ 1 ][ 2 ].disable, tuples[ 2 ][ 2 ].lock ); |
|
} |
|
|
|
|
|
deferred[ tuple[ 0 ] ] = function() { |
|
deferred[ tuple[ 0 ] + "With" ]( this === deferred ? promise : this, arguments ); |
|
return this; |
|
}; |
|
deferred[ tuple[ 0 ] + "With" ] = list.fireWith; |
|
} ); |
|
|
|
|
|
promise.promise( deferred ); |
|
|
|
|
|
if ( func ) { |
|
func.call( deferred, deferred ); |
|
} |
|
|
|
|
|
return deferred; |
|
}, |
|
|
|
|
|
when: function( subordinate ) { |
|
var i = 0, |
|
resolveValues = slice.call( arguments ), |
|
length = resolveValues.length, |
|
|
|
|
|
remaining = length !== 1 || |
|
( subordinate && jQuery.isFunction( subordinate.promise ) ) ? length : 0, |
|
|
|
|
|
|
|
deferred = remaining === 1 ? subordinate : jQuery.Deferred(), |
|
|
|
|
|
updateFunc = function( i, contexts, values ) { |
|
return function( value ) { |
|
contexts[ i ] = this; |
|
values[ i ] = arguments.length > 1 ? slice.call( arguments ) : value; |
|
if ( values === progressValues ) { |
|
deferred.notifyWith( contexts, values ); |
|
} else if ( !( --remaining ) ) { |
|
deferred.resolveWith( contexts, values ); |
|
} |
|
}; |
|
}, |
|
|
|
progressValues, progressContexts, resolveContexts; |
|
|
|
|
|
if ( length > 1 ) { |
|
progressValues = new Array( length ); |
|
progressContexts = new Array( length ); |
|
resolveContexts = new Array( length ); |
|
for ( ; i < length; i++ ) { |
|
if ( resolveValues[ i ] && jQuery.isFunction( resolveValues[ i ].promise ) ) { |
|
resolveValues[ i ].promise() |
|
.progress( updateFunc( i, progressContexts, progressValues ) ) |
|
.done( updateFunc( i, resolveContexts, resolveValues ) ) |
|
.fail( deferred.reject ); |
|
} else { |
|
--remaining; |
|
} |
|
} |
|
} |
|
|
|
|
|
if ( !remaining ) { |
|
deferred.resolveWith( resolveContexts, resolveValues ); |
|
} |
|
|
|
return deferred.promise(); |
|
} |
|
} ); |
|
|
|
return jQuery; |
|
} ); |
|
|