2013-08-15 18:15:49 +00:00
|
|
|
define([
|
|
|
|
"./core",
|
|
|
|
"./var/slice",
|
|
|
|
"./callbacks"
|
|
|
|
], function( jQuery, slice ) {
|
|
|
|
|
2011-03-03 18:38:06 +00:00
|
|
|
jQuery.extend({
|
|
|
|
|
2011-09-12 15:37:41 +00:00
|
|
|
Deferred: function( func ) {
|
2012-04-25 16:24:34 +00:00
|
|
|
var tuples = [
|
2012-04-26 20:08:17 +00:00
|
|
|
// action, add listener, listener list, final state
|
|
|
|
[ "resolve", "done", jQuery.Callbacks("once memory"), "resolved" ],
|
|
|
|
[ "reject", "fail", jQuery.Callbacks("once memory"), "rejected" ],
|
2012-04-25 16:24:34 +00:00
|
|
|
[ "notify", "progress", jQuery.Callbacks("memory") ]
|
|
|
|
],
|
2011-10-12 00:23:56 +00:00
|
|
|
state = "pending",
|
2011-09-21 15:00:55 +00:00
|
|
|
promise = {
|
2011-10-12 00:23:56 +00:00
|
|
|
state: function() {
|
|
|
|
return state;
|
|
|
|
},
|
2011-09-12 15:37:41 +00:00
|
|
|
always: function() {
|
2012-04-25 16:24:34 +00:00
|
|
|
deferred.done( arguments ).fail( arguments );
|
2011-11-09 00:02:29 +00:00
|
|
|
return this;
|
2011-03-03 18:38:06 +00:00
|
|
|
},
|
2012-04-25 16:24:34 +00:00
|
|
|
then: function( /* fnDone, fnFail, fnProgress */ ) {
|
|
|
|
var fns = arguments;
|
2011-09-12 15:37:41 +00:00
|
|
|
return jQuery.Deferred(function( newDefer ) {
|
2012-04-25 16:24:34 +00:00
|
|
|
jQuery.each( tuples, function( i, tuple ) {
|
2013-08-02 00:04:49 +00:00
|
|
|
var fn = jQuery.isFunction( fns[ i ] ) && fns[ i ];
|
2012-04-25 16:24:34 +00:00
|
|
|
// deferred[ done | fail | progress ] for forwarding actions to newDefer
|
2013-01-08 15:27:13 +00:00
|
|
|
deferred[ tuple[1] ](function() {
|
|
|
|
var returned = fn && fn.apply( this, arguments );
|
|
|
|
if ( returned && jQuery.isFunction( returned.promise ) ) {
|
|
|
|
returned.promise()
|
|
|
|
.done( newDefer.resolve )
|
|
|
|
.fail( newDefer.reject )
|
|
|
|
.progress( newDefer.notify );
|
|
|
|
} else {
|
2013-08-02 00:04:49 +00:00
|
|
|
newDefer[ tuple[ 0 ] + "With" ]( this === promise ? newDefer.promise() : this, fn ? [ returned ] : arguments );
|
2013-01-08 15:27:13 +00:00
|
|
|
}
|
|
|
|
});
|
2011-09-12 15:37:41 +00:00
|
|
|
});
|
2012-04-25 16:24:34 +00:00
|
|
|
fns = null;
|
2011-09-12 15:37:41 +00:00
|
|
|
}).promise();
|
2011-03-03 18:38:06 +00:00
|
|
|
},
|
2011-09-12 15:37:41 +00:00
|
|
|
// Get a promise for this deferred
|
|
|
|
// If obj is provided, the promise aspect is added to the object
|
|
|
|
promise: function( obj ) {
|
2012-09-13 13:44:12 +00:00
|
|
|
return obj != null ? jQuery.extend( obj, promise ) : promise;
|
2011-03-03 18:38:06 +00:00
|
|
|
}
|
2011-09-21 15:00:55 +00:00
|
|
|
},
|
2012-04-25 16:24:34 +00:00
|
|
|
deferred = {};
|
2011-09-21 15:00:55 +00:00
|
|
|
|
2012-04-01 23:29:39 +00:00
|
|
|
// Keep pipe for back-compat
|
|
|
|
promise.pipe = promise.then;
|
|
|
|
|
2012-04-25 16:24:34 +00:00
|
|
|
// Add list-specific methods
|
|
|
|
jQuery.each( tuples, function( i, tuple ) {
|
2012-04-26 20:08:17 +00:00
|
|
|
var list = tuple[ 2 ],
|
|
|
|
stateString = tuple[ 3 ];
|
2012-04-01 23:29:39 +00:00
|
|
|
|
2012-04-25 16:24:34 +00:00
|
|
|
// promise[ done | fail | progress ] = list.add
|
|
|
|
promise[ tuple[1] ] = list.add;
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2012-04-25 16:24:34 +00:00
|
|
|
// Handle state
|
2012-04-28 21:06:48 +00:00
|
|
|
if ( stateString ) {
|
2012-04-25 16:24:34 +00:00
|
|
|
list.add(function() {
|
2012-04-26 20:08:17 +00:00
|
|
|
// state = [ resolved | rejected ]
|
2012-04-25 16:24:34 +00:00
|
|
|
state = stateString;
|
2012-04-26 20:08:17 +00:00
|
|
|
|
|
|
|
// [ reject_list | resolve_list ].disable; progress_list.lock
|
|
|
|
}, tuples[ i ^ 1 ][ 2 ].disable, tuples[ 2 ][ 2 ].lock );
|
2012-04-25 16:24:34 +00:00
|
|
|
}
|
|
|
|
|
2012-12-07 14:14:12 +00:00
|
|
|
// deferred[ resolve | reject | notify ]
|
|
|
|
deferred[ tuple[0] ] = function() {
|
2013-01-08 15:27:13 +00:00
|
|
|
deferred[ tuple[0] + "With" ]( this === deferred ? promise : this, arguments );
|
2012-12-07 14:14:12 +00:00
|
|
|
return this;
|
|
|
|
};
|
2012-04-25 16:24:34 +00:00
|
|
|
deferred[ tuple[0] + "With" ] = list.fireWith;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Make the deferred a promise
|
|
|
|
promise.promise( deferred );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
|
|
|
// Call given func if any
|
|
|
|
if ( func ) {
|
|
|
|
func.call( deferred, deferred );
|
|
|
|
}
|
2011-09-12 15:37:41 +00:00
|
|
|
|
|
|
|
// All done!
|
2011-03-03 18:38:06 +00:00
|
|
|
return deferred;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Deferred helper
|
2012-04-25 16:24:34 +00:00
|
|
|
when: function( subordinate /* , ..., subordinateN */ ) {
|
|
|
|
var i = 0,
|
2013-08-15 18:15:49 +00:00
|
|
|
resolveValues = slice.call( arguments ),
|
2012-04-25 16:24:34 +00:00
|
|
|
length = resolveValues.length,
|
|
|
|
|
|
|
|
// the count of uncompleted subordinates
|
|
|
|
remaining = length !== 1 || ( subordinate && jQuery.isFunction( subordinate.promise ) ) ? length : 0,
|
|
|
|
|
|
|
|
// the master Deferred. If resolveValues consist of only a single Deferred, just use that.
|
|
|
|
deferred = remaining === 1 ? subordinate : jQuery.Deferred(),
|
|
|
|
|
|
|
|
// Update function for both resolve and progress values
|
2012-05-12 19:41:39 +00:00
|
|
|
updateFunc = function( i, contexts, values ) {
|
2012-04-25 16:24:34 +00:00
|
|
|
return function( value ) {
|
2012-05-12 19:41:39 +00:00
|
|
|
contexts[ i ] = this;
|
2013-08-15 18:15:49 +00:00
|
|
|
values[ i ] = arguments.length > 1 ? slice.call( arguments ) : value;
|
2013-11-07 20:14:23 +00:00
|
|
|
if ( values === progressValues ) {
|
2012-05-12 19:41:39 +00:00
|
|
|
deferred.notifyWith( contexts, values );
|
2013-11-07 20:14:23 +00:00
|
|
|
|
|
|
|
} else if ( !(--remaining) ) {
|
2012-05-12 19:41:39 +00:00
|
|
|
deferred.resolveWith( contexts, values );
|
2012-04-25 16:24:34 +00:00
|
|
|
}
|
|
|
|
};
|
2012-05-12 19:41:39 +00:00
|
|
|
},
|
2012-06-04 16:48:18 +00:00
|
|
|
|
2012-05-12 19:41:39 +00:00
|
|
|
progressValues, progressContexts, resolveContexts;
|
2012-04-25 16:24:34 +00:00
|
|
|
|
|
|
|
// add listeners to Deferred subordinates; treat others as resolved
|
2011-03-03 18:38:06 +00:00
|
|
|
if ( length > 1 ) {
|
2012-05-12 19:41:39 +00:00
|
|
|
progressValues = new Array( length );
|
|
|
|
progressContexts = new Array( length );
|
|
|
|
resolveContexts = new Array( length );
|
2011-10-26 18:58:05 +00:00
|
|
|
for ( ; i < length; i++ ) {
|
2012-04-25 16:24:34 +00:00
|
|
|
if ( resolveValues[ i ] && jQuery.isFunction( resolveValues[ i ].promise ) ) {
|
|
|
|
resolveValues[ i ].promise()
|
2012-05-12 19:41:39 +00:00
|
|
|
.done( updateFunc( i, resolveContexts, resolveValues ) )
|
2012-04-25 16:24:34 +00:00
|
|
|
.fail( deferred.reject )
|
2012-05-12 19:41:39 +00:00
|
|
|
.progress( updateFunc( i, progressContexts, progressValues ) );
|
2011-03-03 18:38:06 +00:00
|
|
|
} else {
|
2012-04-25 16:24:34 +00:00
|
|
|
--remaining;
|
2011-03-03 18:38:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-25 16:24:34 +00:00
|
|
|
|
|
|
|
// if we're not waiting on anything, resolve the master
|
|
|
|
if ( !remaining ) {
|
2012-05-12 19:41:39 +00:00
|
|
|
deferred.resolveWith( resolveContexts, resolveValues );
|
2012-04-25 16:24:34 +00:00
|
|
|
}
|
|
|
|
|
2012-05-12 19:41:39 +00:00
|
|
|
return deferred.promise();
|
2011-03-03 18:38:06 +00:00
|
|
|
}
|
|
|
|
});
|
2013-09-09 01:25:27 +00:00
|
|
|
|
|
|
|
return jQuery;
|
2013-08-15 18:15:49 +00:00
|
|
|
});
|