Revert "Adds an invert method to promises that returns a "inverted" promise that is resolved when the underlying deferred is rejected and rejected when the underlying deferred is resolved."

This reverts commit 4e97543051.
This commit is contained in:
jaubourg 2011-02-18 18:06:26 +01:00
parent 752db8fffe
commit a43a5ca9cf
2 changed files with 17 additions and 75 deletions

View file

@ -56,16 +56,8 @@ var jQuery = function( selector, context ) {
// The deferred used on DOM ready
readyList,
// Promise methods (with equivalent for invert)
promiseMethods = {
then: 0, // will be overwritten for invert
done: "fail",
fail: "done",
isResolved: "isRejected",
isRejected: "isResolved",
promise: "invert",
invert: "promise"
},
// Promise methods
promiseMethods = "then done fail isResolved isRejected promise".split( " " ),
// The ready event handler
DOMContentLoaded,
@ -885,9 +877,8 @@ jQuery.extend({
Deferred: function( func ) {
var deferred = jQuery._Deferred(),
failDeferred = jQuery._Deferred(),
promise,
invert;
// Add errorDeferred methods, then, promise and invert
promise;
// Add errorDeferred methods, then and promise
jQuery.extend( deferred, {
then: function( doneCallbacks, failCallbacks ) {
deferred.done( doneCallbacks ).fail( failCallbacks );
@ -899,35 +890,18 @@ jQuery.extend({
isRejected: failDeferred.isResolved,
// Get a promise for this deferred
// If obj is provided, the promise aspect is added to the object
promise: function( obj ) {
promise: function( obj , i /* internal */ ) {
if ( obj == null ) {
if ( promise ) {
return promise;
}
promise = obj = {};
}
for( var methodName in promiseMethods ) {
obj[ methodName ] = deferred[ methodName ];
i = promiseMethods.length;
while( i-- ) {
obj[ promiseMethods[ i ] ] = deferred[ promiseMethods[ i ] ];
}
return obj;
},
// Get the invert promise for this deferred
// If obj is provided, the invert promise aspect is added to the object
invert: function( obj ) {
if ( obj == null ) {
if ( invert ) {
return invert;
}
invert = obj = {};
}
for( var methodName in promiseMethods ) {
obj[ methodName ] = promiseMethods[ methodName ] && deferred[ promiseMethods[methodName] ];
}
obj.then = invert.then || function( doneCallbacks, failCallbacks ) {
deferred.done( failCallbacks ).fail( doneCallbacks );
return this;
};
return obj;
}
} );
// Make sure only one callback list will be used

View file

@ -554,8 +554,8 @@ test("toArray()", function() {
expect(1);
same( jQuery("#main p").toArray(),
q("firstp","ap","sndp","en","sap","first"),
"Convert jQuery object to an Array" );
});
"Convert jQuery object to an Array" )
})
test("get(Number)", function() {
expect(2);
@ -567,7 +567,7 @@ test("get(-Number)",function() {
expect(2);
equals( jQuery("p").get(-1), document.getElementById("first"), "Get a single element with negative index" );
strictEqual( jQuery("#firstp").get(-2), undefined, "Try get with index negative index larger then elements count" );
});
})
test("each(Function)", function() {
expect(1);
@ -1009,7 +1009,7 @@ test("jQuery._Deferred()", function() {
test("jQuery.Deferred()", function() {
expect( 20 );
expect( 10 );
jQuery.Deferred( function( defer ) {
strictEqual( this , defer , "Defer passed as this & first argument" );
@ -1049,26 +1049,10 @@ test("jQuery.Deferred()", function() {
ok( true , "Error on reject (new)" );
});
strictEqual( jQuery.Deferred().resolve( "test" ).invert().then(null,function(value) {
strictEqual( value, "test", "Resolved deferred => then fail callback called" );
}).fail(function( value ) {
strictEqual( value, "test", "Resolved deferred => fail callback called" );
}).isRejected(), true, "Invert promise is rejected when deferred is resolved" );
strictEqual( jQuery.Deferred().reject( "test" ).invert().then(function(value) {
strictEqual( value, "test", "Rejected deferred => then done callback called" );
}).done(function( value ) {
strictEqual( value, "test", "Rejected deferred => done callback called" );
}).isResolved(), true, "Invert promise is resolved when deferred is rejected" );
var tmp = jQuery.Deferred();
strictEqual( tmp.promise() , tmp.promise() , "Test deferred always return same promise" );
strictEqual( tmp.invert() , tmp.invert() , "Test deferred always return same invert" );
strictEqual( tmp.promise() , tmp.promise().promise() , "Test deferred's promise always return same promise as deferred" );
strictEqual( tmp.promise() , tmp.invert().invert() , "Test deferred's promise is the same as double invert" );
strictEqual( tmp.invert() , tmp.invert().promise() , "Test deferred's invert always return same invert as deferred as a promise" );
strictEqual( tmp.invert() , tmp.promise().invert() , "Test deferred's promise always return same invert as deferred" );
});
test("jQuery.when()", function() {
@ -1116,7 +1100,7 @@ test("jQuery.when()", function() {
test("jQuery.when() - joined", function() {
expect(14);
expect(8);
jQuery.when( 1, 2, 3 ).done( function( a, b, c ) {
strictEqual( a , 1 , "Test first param is first resolved value - non-observables" );
@ -1137,28 +1121,12 @@ test("jQuery.when() - joined", function() {
ok( false , "Test the created deferred was resolved - resolved observable");
});
jQuery.when( 1 , successDeferred.invert() , 3 ).fail( function( a, b, c ) {
strictEqual( a , 1 , "Test first param is first rejected value - resolved observable inverted" );
same( b , 2 , "Test second param is second rejected value - resolved observable inverted" );
strictEqual( c , 3 , "Test third param is third rejected value - resolved observable inverted" );
}).done( function() {
ok( false , "Test the inverted deferred was rejected - resolved observable inverted");
});
jQuery.when( 1 , errorDeferred , 3 ).done( function() {
ok( false , "Test the created deferred was rejected - rejected observable");
}).fail( function( error , errorParam ) {
strictEqual( error , "error" , "Test first param is first rejected value - rejected observable" );
strictEqual( errorParam , "errorParam" , "Test second param is second rejected value - rejected observable" );
});
jQuery.when( 1 , errorDeferred.invert() , 3 ).fail( function() {
ok( false , "Test the inverted deferred was resolved - rejected observable inverted");
}).done( function( a , b , c ) {
strictEqual( a , 1 , "Test first param is first resolved value - rejected observable inverted" );
same( b , [ "error", "errorParam" ] , "Test second param is second resolved value - rejected observable inverted" );
strictEqual( c , 3 , "Test third param is third resolved value - rejected observable inverted" );
});
});
test("jQuery.sub() - Static Methods", function(){
@ -1175,16 +1143,16 @@ test("jQuery.sub() - Static Methods", function(){
}
});
Subclass.fn.extend({subClassMethod: function() { return this;}});
//Test Simple Subclass
ok(Subclass.topLevelMethod() === false, 'Subclass.topLevelMethod thought debug was true');
ok(Subclass.config.locale == 'en_US', Subclass.config.locale + ' is wrong!');
same(Subclass.config.test, undefined, 'Subclass.config.test is set incorrectly');
equal(jQuery.ajax, Subclass.ajax, 'The subclass failed to get all top level methods');
//Create a SubSubclass
var SubSubclass = Subclass.sub();
//Make Sure the SubSubclass inherited properly
ok(SubSubclass.topLevelMethod() === false, 'SubSubclass.topLevelMethod thought debug was true');
ok(SubSubclass.config.locale == 'en_US', SubSubclass.config.locale + ' is wrong!');
@ -1201,7 +1169,7 @@ test("jQuery.sub() - Static Methods", function(){
ok(SubSubclass.config.locale == 'es_MX', SubSubclass.config.locale + ' is wrong!');
ok(SubSubclass.config.test == 'worked', 'SubSubclass.config.test is set incorrectly');
notEqual(jQuery.ajax, SubSubclass.ajax, 'The subsubclass failed to get all top level methods');
//This shows that the modifications to the SubSubClass did not bubble back up to it's superclass
ok(Subclass.topLevelMethod() === false, 'Subclass.topLevelMethod thought debug was true');
ok(Subclass.config.locale == 'en_US', Subclass.config.locale + ' is wrong!');