Fixes some coding style issues in core.js. In jQuery.fn.ready(), simplifies the whole code (for better backward compatibility) and removes redefinition of the function itself (in order not to clash with proxying). Also, in jQuery.when(), removes unnecessary closure and early rejection test and makes use of then instead of done/fail (better interoperability).

This commit is contained in:
jaubourg 2011-01-23 05:01:36 +01:00
parent bea4815294
commit 30082d9eba

View file

@ -247,12 +247,14 @@ jQuery.fn = jQuery.prototype = {
return jQuery.each( this, callback, args );
},
ready: function() {
ready: function( fn ) {
// Attach the listeners
jQuery.bindReady();
// Change ready & apply
return ( jQuery.fn.ready = readyList.done ).apply( this , arguments );
// Add the callback
readyList.done( fn );
return this;
},
eq: function( i ) {
@ -797,7 +799,6 @@ jQuery.extend({
// Create a simple deferred (one callbacks list)
_Deferred: function() {
var // callbacks list
callbacks = [],
// stored [ context , args ]
@ -811,21 +812,17 @@ jQuery.extend({
// done( f1, f2, ...)
done: 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 );
@ -835,21 +832,17 @@ jQuery.extend({
callbacks.push( elem );
}
}
if ( _fired ) {
deferred.resolveWith( _fired[ 0 ], _fired[ 1 ] );
}
}
return this;
},
// resolve with given context and args
resolveWith: function( context, args ) {
if ( !cancelled && !fired && !firing ) {
firing = 1;
try {
while( callbacks[ 0 ] ) {
callbacks.shift().apply( context, args );
@ -886,16 +879,12 @@ jQuery.extend({
},
// Full fledged deferred (two callbacks list)
// Typical success/error system
Deferred: function( func ) {
var deferred = jQuery._Deferred(),
failDeferred = jQuery._Deferred(),
promise;
// Add errorDeferred methods, then and promise
jQuery.extend( deferred, {
then: function( doneCallbacks, failCallbacks ) {
deferred.done( doneCallbacks ).fail( failCallbacks );
return this;
@ -906,8 +895,7 @@ jQuery.extend({
isRejected: failDeferred.isResolved,
// Get a promise for this deferred
// If obj is provided, the promise aspect is added to the object
// (i is used internally)
promise: function( obj , i ) {
promise: function( obj , i /* internal */ ) {
if ( obj == null ) {
if ( promise ) {
return promise;
@ -920,20 +908,15 @@ jQuery.extend({
}
return obj;
}
} );
// Make sure only one callback list will be used
deferred.then( failDeferred.cancel, deferred.cancel );
// Unexpose cancel
delete deferred.cancel;
// Call given func if any
if ( func ) {
func.call( deferred, deferred );
}
return deferred;
},
@ -950,16 +933,13 @@ jQuery.extend({
if ( length > 1 ) {
resolveArray = new Array( length );
jQuery.each( args, function( index, element, args ) {
jQuery.when( element ).done( function( value ) {
jQuery.when( element ).then( function( value ) {
args = arguments;
resolveArray[ index ] = args.length > 1 ? slice.call( args, 0 ) : value;
if( ! --length ) {
deferred.resolveWith( promise, resolveArray );
}
}).fail( function() {
deferred.rejectWith( promise, arguments );
});
return !deferred.isRejected();
}, deferred.reject );
} );
} else if ( deferred !== object ) {
deferred.resolve( object );