From 139135a98aab1c422e5ae05b026535a40d58328f Mon Sep 17 00:00:00 2001 From: jaubourg Date: Fri, 1 Jul 2011 01:51:50 +0200 Subject: [PATCH] Fixes #9446. Context is properly propagated using pipe. If context was the original deferred, then context is updated to next deferred in the chain. Unit tests added. --- src/deferred.js | 2 +- test/unit/deferred.js | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/deferred.js b/src/deferred.js index 5cc5fb5be..e543f1518 100644 --- a/src/deferred.js +++ b/src/deferred.js @@ -122,7 +122,7 @@ jQuery.extend({ if ( returned && jQuery.isFunction( returned.promise ) ) { returned.promise().then( newDefer.resolve, newDefer.reject ); } else { - newDefer[ action ]( returned ); + newDefer[ action + "With" ]( this === deferred ? newDefer : this, [ returned ] ); } }); } else { diff --git a/test/unit/deferred.js b/test/unit/deferred.js index 89c9c612d..fbe290707 100644 --- a/test/unit/deferred.js +++ b/test/unit/deferred.js @@ -275,6 +275,33 @@ test( "jQuery.Deferred.pipe - deferred (fail)", function() { strictEqual( value3, 6, "result of filter ok" ); }); +test( "jQuery.Deferred.pipe - context", function() { + + expect(4); + + var context = {}; + + jQuery.Deferred().resolveWith( context, [ 2 ] ).pipe(function( value ) { + return value * 3; + }).done(function( value ) { + strictEqual( this, context, "custom context correctly propagated" ); + strictEqual( value, 6, "proper value received" ); + }); + + var defer = jQuery.Deferred(), + piped = defer.pipe(function( value ) { + return value * 3; + }); + + defer.resolve( 2 ); + + piped.done(function( value ) { + strictEqual( this.promise(), piped, "default context gets updated to latest defer in the chain" ); + strictEqual( value, 6, "proper value received" ); + }); +}); + + test( "jQuery.when" , function() { expect( 23 );