Skip to content

Commit

Permalink
Propagate context of returned deferred object in Deferred.then(). Fix…
Browse files Browse the repository at this point in the history
…es #13160.
  • Loading branch information
nanto authored and jaubourg committed Jan 8, 2013
2 parents 18c376a + 412d910 commit 2f6b3f8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
29 changes: 13 additions & 16 deletions src/deferred.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,19 @@ jQuery.extend({
return jQuery.Deferred(function( newDefer ) {
jQuery.each( tuples, function( i, tuple ) {
var action = tuple[ 0 ],
fn = fns[ i ];
fn = jQuery.isFunction( fns[ i ] ) && fns[ i ];
// deferred[ done | fail | progress ] for forwarding actions to newDefer
deferred[ tuple[1] ]( jQuery.isFunction( fn ) ?
function() {
var returned = fn.apply( this, arguments );
if ( returned && jQuery.isFunction( returned.promise ) ) {
returned.promise()
.done( newDefer.resolve )
.fail( newDefer.reject )
.progress( newDefer.notify );
} else {
newDefer[ action + "With" ]( this === promise ? newDefer.promise() : this, [ returned ] );
}
} :
newDefer[ action ]
);
deferred[ tuple[1] ](function() {
var returned = fn && fn.apply( this, arguments );
if ( returned && jQuery.isFunction( returned.promise ) ) {
returned.promise()
.done( newDefer.resolve )
.fail( newDefer.reject )
.progress( newDefer.notify );
} else {
newDefer[ action + "With" ]( this === promise ? newDefer.promise() : this, fn ? [ returned ] : arguments );
}
});
});
fns = null;
}).promise();
Expand Down Expand Up @@ -72,7 +69,7 @@ jQuery.extend({

// deferred[ resolve | reject | notify ]
deferred[ tuple[0] ] = function() {
deferred[ tuple[0] + "With" ]( promise, arguments );
deferred[ tuple[0] + "With" ]( this === deferred ? promise : this, arguments );
return this;
};
deferred[ tuple[0] + "With" ] = list.fireWith;
Expand Down
18 changes: 17 additions & 1 deletion test/unit/deferred.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ test( "jQuery.Deferred.then - deferred (progress)", function() {

test( "jQuery.Deferred.then - context", function() {

expect( 4 );
expect( 7 );

var context = {};

Expand All @@ -284,6 +284,12 @@ test( "jQuery.Deferred.then - context", function() {
strictEqual( value, 6, "proper value received" );
});

jQuery.Deferred().resolve().then(function() {
return jQuery.Deferred().resolveWith(context);
}).done(function() {
strictEqual( this, context, "custom context of returned deferred correctly propagated" );
});

var defer = jQuery.Deferred(),
piped = defer.then(function( value ) {
return value * 3;
Expand All @@ -295,6 +301,16 @@ test( "jQuery.Deferred.then - context", function() {
strictEqual( this, piped, "default context gets updated to latest promise in the chain" );
strictEqual( value, 6, "proper value received" );
});

var defer2 = jQuery.Deferred(),
piped2 = defer2.then();

defer2.resolve( 2 );

piped2.done(function( value ) {
strictEqual( this, piped2, "default context gets updated to latest promise in the chain (without passing function)" );
strictEqual( value, 2, "proper value received (without passing function)" );
});
});

test( "jQuery.when", function() {
Expand Down

0 comments on commit 2f6b3f8

Please sign in to comment.