2011-03-03 18:38:06 +00:00
|
|
|
(function( jQuery ) {
|
|
|
|
|
2011-09-21 15:00:55 +00:00
|
|
|
var // Static reference to slice
|
2011-03-03 18:38:06 +00:00
|
|
|
sliceDeferred = [].slice;
|
|
|
|
|
|
|
|
jQuery.extend({
|
|
|
|
|
2011-09-12 15:37:41 +00:00
|
|
|
Deferred: function( func ) {
|
2012-04-25 16:24:34 +00:00
|
|
|
var tuples = [
|
|
|
|
// action, add listener, listener list
|
|
|
|
[ "resolve", "done", jQuery.Callbacks("once memory") ],
|
|
|
|
[ "reject", "fail", jQuery.Callbacks("once memory") ],
|
|
|
|
[ "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 ) {
|
|
|
|
var action = tuple[ 0 ],
|
|
|
|
fn = fns[ i ];
|
|
|
|
// deferred[ done | fail | progress ] for forwarding actions to newDefer
|
|
|
|
deferred[ tuple[1] ]( jQuery.isFunction( fn ) ?
|
|
|
|
function() {
|
|
|
|
var returned = fn.apply( this, arguments );
|
2011-09-12 15:37:41 +00:00
|
|
|
if ( returned && jQuery.isFunction( returned.promise ) ) {
|
2012-04-25 16:24:34 +00:00
|
|
|
returned.promise()
|
|
|
|
.done( newDefer.resolve )
|
|
|
|
.fail( newDefer.reject )
|
|
|
|
.progress( newDefer.notify );
|
2011-09-12 15:37:41 +00:00
|
|
|
} else {
|
|
|
|
newDefer[ action + "With" ]( this === deferred ? newDefer : this, [ returned ] );
|
|
|
|
}
|
2012-04-25 16:24:34 +00:00
|
|
|
} :
|
|
|
|
newDefer[ action ]
|
|
|
|
);
|
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-04-25 16:24:34 +00:00
|
|
|
return typeof obj === "object" ? 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 ) {
|
|
|
|
var list = tuple[ 2 ], stateString;
|
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
|
|
|
|
if ( i < 2 ) {
|
|
|
|
stateString = tuple[ 0 ].replace( /e?$/, "ed" );
|
|
|
|
list.add(function() {
|
|
|
|
state = stateString;
|
|
|
|
tuples[ i ^ 1 ][ 2 ].disable();
|
|
|
|
tuples[ 2 ][ 2 ].lock();
|
|
|
|
});
|
|
|
|
promise[ "isR" + stateString.substr( 1 ) ] = list.fired;
|
|
|
|
}
|
|
|
|
|
|
|
|
// deferred[ resolve | reject | notify ] = list.fire
|
|
|
|
deferred[ tuple[0] ] = list.fire;
|
|
|
|
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,
|
|
|
|
resolveValues = sliceDeferred.call( arguments ),
|
|
|
|
length = resolveValues.length,
|
|
|
|
progressValues = new Array( 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(),
|
|
|
|
promise = deferred.promise(),
|
|
|
|
|
|
|
|
// Update function for both resolve and progress values
|
|
|
|
updateFunc = function( i, arr ) {
|
|
|
|
return function( value ) {
|
|
|
|
arr[ i ] = arguments.length > 1 ? sliceDeferred.call( arguments ) : value;
|
|
|
|
if( arr === progressValues ) {
|
|
|
|
deferred.notifyWith( promise, arr );
|
|
|
|
} else if ( !( --remaining ) ) {
|
|
|
|
deferred.resolveWith( promise, arr );
|
|
|
|
}
|
|
|
|
};
|
2011-09-12 15:37:41 +00:00
|
|
|
};
|
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 ) {
|
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()
|
|
|
|
.done( updateFunc( i, resolveValues ) )
|
|
|
|
.fail( deferred.reject )
|
|
|
|
.progress( updateFunc( i, 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 ) {
|
|
|
|
deferred.resolveWith( deferred, resolveValues );
|
|
|
|
}
|
|
|
|
|
2011-09-12 15:37:41 +00:00
|
|
|
return promise;
|
2011-03-03 18:38:06 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-09-12 15:37:41 +00:00
|
|
|
})( jQuery );
|