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:
parent
bea4815294
commit
30082d9eba
72
src/core.js
72
src/core.js
|
@ -134,7 +134,7 @@ jQuery.fn = jQuery.prototype = {
|
||||||
}
|
}
|
||||||
|
|
||||||
return jQuery.merge( this, selector );
|
return jQuery.merge( this, selector );
|
||||||
|
|
||||||
// HANDLE: $("#id")
|
// HANDLE: $("#id")
|
||||||
} else {
|
} else {
|
||||||
elem = document.getElementById( match[2] );
|
elem = document.getElementById( match[2] );
|
||||||
|
@ -247,12 +247,14 @@ jQuery.fn = jQuery.prototype = {
|
||||||
return jQuery.each( this, callback, args );
|
return jQuery.each( this, callback, args );
|
||||||
},
|
},
|
||||||
|
|
||||||
ready: function() {
|
ready: function( fn ) {
|
||||||
// Attach the listeners
|
// Attach the listeners
|
||||||
jQuery.bindReady();
|
jQuery.bindReady();
|
||||||
|
|
||||||
// Change ready & apply
|
// Add the callback
|
||||||
return ( jQuery.fn.ready = readyList.done ).apply( this , arguments );
|
readyList.done( fn );
|
||||||
|
|
||||||
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
eq: function( i ) {
|
eq: function( i ) {
|
||||||
|
@ -399,7 +401,7 @@ jQuery.extend({
|
||||||
}
|
}
|
||||||
|
|
||||||
// If there are functions bound, to execute
|
// If there are functions bound, to execute
|
||||||
readyList.resolveWith( document , [ jQuery ] );
|
readyList.resolveWith( document, [ jQuery ] );
|
||||||
|
|
||||||
// Trigger any bound ready events
|
// Trigger any bound ready events
|
||||||
if ( jQuery.fn.trigger ) {
|
if ( jQuery.fn.trigger ) {
|
||||||
|
@ -797,7 +799,6 @@ jQuery.extend({
|
||||||
|
|
||||||
// Create a simple deferred (one callbacks list)
|
// Create a simple deferred (one callbacks list)
|
||||||
_Deferred: function() {
|
_Deferred: function() {
|
||||||
|
|
||||||
var // callbacks list
|
var // callbacks list
|
||||||
callbacks = [],
|
callbacks = [],
|
||||||
// stored [ context , args ]
|
// stored [ context , args ]
|
||||||
|
@ -810,53 +811,45 @@ jQuery.extend({
|
||||||
deferred = {
|
deferred = {
|
||||||
|
|
||||||
// done( f1, f2, ...)
|
// done( f1, f2, ...)
|
||||||
done: function () {
|
done: function() {
|
||||||
|
if ( !cancelled ) {
|
||||||
if ( ! cancelled ) {
|
|
||||||
|
|
||||||
var args = arguments,
|
var args = arguments,
|
||||||
i,
|
i,
|
||||||
length,
|
length,
|
||||||
elem,
|
elem,
|
||||||
type,
|
type,
|
||||||
_fired;
|
_fired;
|
||||||
|
|
||||||
if ( fired ) {
|
if ( fired ) {
|
||||||
_fired = fired;
|
_fired = fired;
|
||||||
fired = 0;
|
fired = 0;
|
||||||
}
|
}
|
||||||
|
for ( i = 0, length = args.length; i < length; i++ ) {
|
||||||
for ( i = 0, length = args.length ; i < length ; i++ ) {
|
|
||||||
elem = args[ i ];
|
elem = args[ i ];
|
||||||
type = jQuery.type( elem );
|
type = jQuery.type( elem );
|
||||||
if ( type === "array" ) {
|
if ( type === "array" ) {
|
||||||
deferred.done.apply( deferred , elem );
|
deferred.done.apply( deferred, elem );
|
||||||
} else if ( type === "function" ) {
|
} else if ( type === "function" ) {
|
||||||
callbacks.push( elem );
|
callbacks.push( elem );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( _fired ) {
|
if ( _fired ) {
|
||||||
deferred.resolveWith( _fired[ 0 ] , _fired[ 1 ] );
|
deferred.resolveWith( _fired[ 0 ], _fired[ 1 ] );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
// resolve with given context and args
|
// resolve with given context and args
|
||||||
resolveWith: function( context , args ) {
|
resolveWith: function( context, args ) {
|
||||||
if ( ! cancelled && ! fired && ! firing ) {
|
if ( !cancelled && !fired && !firing ) {
|
||||||
|
|
||||||
firing = 1;
|
firing = 1;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
while( callbacks[ 0 ] ) {
|
while( callbacks[ 0 ] ) {
|
||||||
callbacks.shift().apply( context , args );
|
callbacks.shift().apply( context, args );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
fired = [ context , args ];
|
fired = [ context, args ];
|
||||||
firing = 0;
|
firing = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -865,7 +858,7 @@ jQuery.extend({
|
||||||
|
|
||||||
// resolve with this as context and given arguments
|
// resolve with this as context and given arguments
|
||||||
resolve: function() {
|
resolve: function() {
|
||||||
deferred.resolveWith( jQuery.isFunction( this.promise ) ? this.promise() : this , arguments );
|
deferred.resolveWith( jQuery.isFunction( this.promise ) ? this.promise() : this, arguments );
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -886,17 +879,13 @@ jQuery.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
// Full fledged deferred (two callbacks list)
|
// Full fledged deferred (two callbacks list)
|
||||||
// Typical success/error system
|
|
||||||
Deferred: function( func ) {
|
Deferred: function( func ) {
|
||||||
|
|
||||||
var deferred = jQuery._Deferred(),
|
var deferred = jQuery._Deferred(),
|
||||||
failDeferred = jQuery._Deferred(),
|
failDeferred = jQuery._Deferred(),
|
||||||
promise;
|
promise;
|
||||||
|
|
||||||
// Add errorDeferred methods, then and promise
|
// Add errorDeferred methods, then and promise
|
||||||
jQuery.extend( deferred , {
|
jQuery.extend( deferred, {
|
||||||
|
then: function( doneCallbacks, failCallbacks ) {
|
||||||
then: function( doneCallbacks , failCallbacks ) {
|
|
||||||
deferred.done( doneCallbacks ).fail( failCallbacks );
|
deferred.done( doneCallbacks ).fail( failCallbacks );
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
@ -906,8 +895,7 @@ jQuery.extend({
|
||||||
isRejected: failDeferred.isResolved,
|
isRejected: failDeferred.isResolved,
|
||||||
// Get a promise for this deferred
|
// Get a promise for this deferred
|
||||||
// If obj is provided, the promise aspect is added to the object
|
// If obj is provided, the promise aspect is added to the object
|
||||||
// (i is used internally)
|
promise: function( obj , i /* internal */ ) {
|
||||||
promise: function( obj , i ) {
|
|
||||||
if ( obj == null ) {
|
if ( obj == null ) {
|
||||||
if ( promise ) {
|
if ( promise ) {
|
||||||
return promise;
|
return promise;
|
||||||
|
@ -920,20 +908,15 @@ jQuery.extend({
|
||||||
}
|
}
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
} );
|
} );
|
||||||
|
|
||||||
// Make sure only one callback list will be used
|
// Make sure only one callback list will be used
|
||||||
deferred.then( failDeferred.cancel , deferred.cancel );
|
deferred.then( failDeferred.cancel, deferred.cancel );
|
||||||
|
|
||||||
// Unexpose cancel
|
// Unexpose cancel
|
||||||
delete deferred.cancel;
|
delete deferred.cancel;
|
||||||
|
|
||||||
// Call given func if any
|
// Call given func if any
|
||||||
if ( func ) {
|
if ( func ) {
|
||||||
func.call( deferred , deferred );
|
func.call( deferred, deferred );
|
||||||
}
|
}
|
||||||
|
|
||||||
return deferred;
|
return deferred;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -950,17 +933,14 @@ jQuery.extend({
|
||||||
if ( length > 1 ) {
|
if ( length > 1 ) {
|
||||||
resolveArray = new Array( length );
|
resolveArray = new Array( length );
|
||||||
jQuery.each( args, function( index, element, args ) {
|
jQuery.each( args, function( index, element, args ) {
|
||||||
jQuery.when( element ).done( function( value ) {
|
jQuery.when( element ).then( function( value ) {
|
||||||
args = arguments;
|
args = arguments;
|
||||||
resolveArray[ index ] = args.length > 1 ? slice.call( args , 0 ) : value;
|
resolveArray[ index ] = args.length > 1 ? slice.call( args, 0 ) : value;
|
||||||
if( ! --length ) {
|
if( ! --length ) {
|
||||||
deferred.resolveWith( promise, resolveArray );
|
deferred.resolveWith( promise, resolveArray );
|
||||||
}
|
}
|
||||||
}).fail( function() {
|
}, deferred.reject );
|
||||||
deferred.rejectWith( promise, arguments );
|
} );
|
||||||
});
|
|
||||||
return !deferred.isRejected();
|
|
||||||
});
|
|
||||||
} else if ( deferred !== object ) {
|
} else if ( deferred !== object ) {
|
||||||
deferred.resolve( object );
|
deferred.resolve( object );
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue