"then" renamed "complete" and new "then" method defined using "complete" and "fail" internally. The API is still symetrical (complete/fail), still promotes AOP but is also now Promise/A compliant. Changed unit tests and ajax module accordingly.
This commit is contained in:
parent
4ab94a578e
commit
cbf591152c
|
@ -496,9 +496,9 @@ jQuery.extend({
|
||||||
|
|
||||||
// Attach deferreds
|
// Attach deferreds
|
||||||
deferred.promise( jXHR );
|
deferred.promise( jXHR );
|
||||||
jXHR.success = jXHR.then;
|
jXHR.success = jXHR.complete;
|
||||||
jXHR.error = jXHR.fail;
|
jXHR.error = jXHR.fail;
|
||||||
jXHR.complete = completeDeferred.then;
|
jXHR.complete = completeDeferred.complete;
|
||||||
|
|
||||||
// Remove hash character (#7531: and string promotion)
|
// Remove hash character (#7531: and string promotion)
|
||||||
s.url = ( "" + s.url ).replace( rhash , "" );
|
s.url = ( "" + s.url ).replace( rhash , "" );
|
||||||
|
|
44
src/core.js
44
src/core.js
|
@ -258,7 +258,7 @@ jQuery.fn = jQuery.prototype = {
|
||||||
jQuery.bindReady();
|
jQuery.bindReady();
|
||||||
|
|
||||||
// Change ready & apply
|
// Change ready & apply
|
||||||
return ( jQuery.fn.ready = readyList.then ).apply( this , arguments );
|
return ( jQuery.fn.ready = readyList.complete ).apply( this , arguments );
|
||||||
},
|
},
|
||||||
|
|
||||||
eq: function( i ) {
|
eq: function( i ) {
|
||||||
|
@ -815,8 +815,8 @@ jQuery.extend({
|
||||||
// the deferred itself
|
// the deferred itself
|
||||||
deferred = {
|
deferred = {
|
||||||
|
|
||||||
// then( f1, f2, ...)
|
// complete( f1, f2, ...)
|
||||||
then: function () {
|
complete: function () {
|
||||||
|
|
||||||
if ( ! cancelled ) {
|
if ( ! cancelled ) {
|
||||||
|
|
||||||
|
@ -836,7 +836,7 @@ jQuery.extend({
|
||||||
elem = args[ i ];
|
elem = args[ i ];
|
||||||
type = jQuery.type( elem );
|
type = jQuery.type( elem );
|
||||||
if ( type === "array" ) {
|
if ( type === "array" ) {
|
||||||
deferred.then.apply( deferred , elem );
|
deferred.complete.apply( deferred , elem );
|
||||||
} else if ( type === "function" ) {
|
} else if ( type === "function" ) {
|
||||||
callbacks.push( elem );
|
callbacks.push( elem );
|
||||||
}
|
}
|
||||||
|
@ -901,27 +901,31 @@ jQuery.extend({
|
||||||
// Add errorDeferred methods and redefine cancel
|
// Add errorDeferred methods and redefine cancel
|
||||||
jQuery.extend( deferred , {
|
jQuery.extend( deferred , {
|
||||||
|
|
||||||
fail: failDeferred.then,
|
then: function( completeCallbacks , failCallbacks ) {
|
||||||
fireReject: failDeferred.fire,
|
deferred.complete( completeCallbacks ).fail( failCallbacks );
|
||||||
reject: failDeferred.resolve,
|
return this;
|
||||||
isRejected: failDeferred.isResolved,
|
},
|
||||||
// Get a promise for this deferred
|
fail: failDeferred.complete,
|
||||||
// If obj is provided, the promise aspect is added to the object
|
fireReject: failDeferred.fire,
|
||||||
promise: function( obj ) {
|
reject: failDeferred.resolve,
|
||||||
obj = obj || {};
|
isRejected: failDeferred.isResolved,
|
||||||
jQuery.each( "then fail isResolved isRejected".split( " " ) , function( _ , method ) {
|
// Get a promise for this deferred
|
||||||
obj[ method ] = deferred[ method ];
|
// If obj is provided, the promise aspect is added to the object
|
||||||
});
|
promise: function( obj ) {
|
||||||
obj.promise = function() {
|
obj = obj || {};
|
||||||
return obj;
|
jQuery.each( "then complete fail isResolved isRejected".split( " " ) , function( _ , method ) {
|
||||||
};
|
obj[ method ] = deferred[ method ];
|
||||||
|
});
|
||||||
|
obj.promise = function() {
|
||||||
return obj;
|
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 ).fail( deferred.cancel );
|
deferred.then( failDeferred.cancel , deferred.cancel );
|
||||||
|
|
||||||
// Unexpose cancel
|
// Unexpose cancel
|
||||||
delete deferred.cancel;
|
delete deferred.cancel;
|
||||||
|
|
|
@ -917,7 +917,7 @@ test("jQuery._Deferred()", function() {
|
||||||
|
|
||||||
test = false;
|
test = false;
|
||||||
|
|
||||||
deferred.then( function( value ) {
|
deferred.complete( function( value ) {
|
||||||
equals( value , "value" , "Test pre-resolve callback" );
|
equals( value , "value" , "Test pre-resolve callback" );
|
||||||
test = true;
|
test = true;
|
||||||
} );
|
} );
|
||||||
|
@ -928,7 +928,7 @@ test("jQuery._Deferred()", function() {
|
||||||
|
|
||||||
test = false;
|
test = false;
|
||||||
|
|
||||||
deferred.then( function( value ) {
|
deferred.complete( function( value ) {
|
||||||
equals( value , "value" , "Test post-resolve callback" );
|
equals( value , "value" , "Test post-resolve callback" );
|
||||||
test = true;
|
test = true;
|
||||||
} );
|
} );
|
||||||
|
@ -939,7 +939,7 @@ test("jQuery._Deferred()", function() {
|
||||||
|
|
||||||
test = true;
|
test = true;
|
||||||
|
|
||||||
deferred.then( function() {
|
deferred.complete( function() {
|
||||||
ok( false , "Cancel was ignored" );
|
ok( false , "Cancel was ignored" );
|
||||||
test = false;
|
test = false;
|
||||||
} );
|
} );
|
||||||
|
@ -949,18 +949,18 @@ test("jQuery._Deferred()", function() {
|
||||||
deferred = jQuery._Deferred().resolve();
|
deferred = jQuery._Deferred().resolve();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
deferred.then( function() {
|
deferred.complete( function() {
|
||||||
throw "Error";
|
throw "Error";
|
||||||
} , function() {
|
} , function() {
|
||||||
ok( true , "Test deferred do not cancel on exception" );
|
ok( true , "Test deferred do not cancel on exception" );
|
||||||
} );
|
} );
|
||||||
} catch( e ) {
|
} catch( e ) {
|
||||||
strictEqual( e , "Error" , "Test deferred propagates exceptions");
|
strictEqual( e , "Error" , "Test deferred propagates exceptions");
|
||||||
deferred.then();
|
deferred.complete();
|
||||||
}
|
}
|
||||||
|
|
||||||
test = "";
|
test = "";
|
||||||
deferred = jQuery._Deferred().then( function() {
|
deferred = jQuery._Deferred().complete( function() {
|
||||||
|
|
||||||
test += "A";
|
test += "A";
|
||||||
|
|
||||||
|
@ -970,13 +970,13 @@ test("jQuery._Deferred()", function() {
|
||||||
|
|
||||||
} ).resolve();
|
} ).resolve();
|
||||||
|
|
||||||
strictEqual( test , "AB" , "Test multiple then parameters" );
|
strictEqual( test , "AB" , "Test multiple complete parameters" );
|
||||||
|
|
||||||
test = "";
|
test = "";
|
||||||
|
|
||||||
deferred.then( function() {
|
deferred.complete( function() {
|
||||||
|
|
||||||
deferred.then( function() {
|
deferred.complete( function() {
|
||||||
|
|
||||||
test += "C";
|
test += "C";
|
||||||
|
|
||||||
|
@ -989,11 +989,11 @@ test("jQuery._Deferred()", function() {
|
||||||
test += "B";
|
test += "B";
|
||||||
} );
|
} );
|
||||||
|
|
||||||
strictEqual( test , "ABC" , "Test then callbacks order" );
|
strictEqual( test , "ABC" , "Test complete callbacks order" );
|
||||||
|
|
||||||
deferred = jQuery._Deferred();
|
deferred = jQuery._Deferred();
|
||||||
|
|
||||||
deferred.fire( jQuery , [ document ] ).then( function( doc ) {
|
deferred.fire( jQuery , [ document ] ).complete( function( doc ) {
|
||||||
ok( this === jQuery && arguments.length === 1 && doc === document , "Test fire context & args" );
|
ok( this === jQuery && arguments.length === 1 && doc === document , "Test fire context & args" );
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1011,13 +1011,13 @@ test("jQuery.Deferred()", function() {
|
||||||
|
|
||||||
jQuery.Deferred().resolve().then( function() {
|
jQuery.Deferred().resolve().then( function() {
|
||||||
ok( true , "Success on resolve" );
|
ok( true , "Success on resolve" );
|
||||||
}).fail( function() {
|
}, function() {
|
||||||
ok( false , "Error on resolve" );
|
ok( false , "Error on resolve" );
|
||||||
});
|
});
|
||||||
|
|
||||||
jQuery.Deferred().reject().then( function() {
|
jQuery.Deferred().reject().then( function() {
|
||||||
ok( false , "Success on reject" );
|
ok( false , "Success on reject" );
|
||||||
}).fail( function() {
|
}, function() {
|
||||||
ok( true , "Error on reject" );
|
ok( true , "Error on reject" );
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1055,7 +1055,7 @@ test("jQuery.when()", function() {
|
||||||
}) ).then( function( value ) {
|
}) ).then( function( value ) {
|
||||||
strictEqual( value , 1 , "Function executed" + ( i > 1 ? " only once" : "" ) );
|
strictEqual( value , 1 , "Function executed" + ( i > 1 ? " only once" : "" ) );
|
||||||
cache = value;
|
cache = value;
|
||||||
}).fail( function() {
|
}, function() {
|
||||||
ok( false , "Fail called" );
|
ok( false , "Fail called" );
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue