Skip to content

Commit 139135a

Browse files
committed
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.
1 parent 5b92a5f commit 139135a

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/deferred.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ jQuery.extend({
122122
if ( returned && jQuery.isFunction( returned.promise ) ) {
123123
returned.promise().then( newDefer.resolve, newDefer.reject );
124124
} else {
125-
newDefer[ action ]( returned );
125+
newDefer[ action + "With" ]( this === deferred ? newDefer : this, [ returned ] );
126126
}
127127
});
128128
} else {

test/unit/deferred.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,33 @@ test( "jQuery.Deferred.pipe - deferred (fail)", function() {
275275
strictEqual( value3, 6, "result of filter ok" );
276276
});
277277

278+
test( "jQuery.Deferred.pipe - context", function() {
279+
280+
expect(4);
281+
282+
var context = {};
283+
284+
jQuery.Deferred().resolveWith( context, [ 2 ] ).pipe(function( value ) {
285+
return value * 3;
286+
}).done(function( value ) {
287+
strictEqual( this, context, "custom context correctly propagated" );
288+
strictEqual( value, 6, "proper value received" );
289+
});
290+
291+
var defer = jQuery.Deferred(),
292+
piped = defer.pipe(function( value ) {
293+
return value * 3;
294+
});
295+
296+
defer.resolve( 2 );
297+
298+
piped.done(function( value ) {
299+
strictEqual( this.promise(), piped, "default context gets updated to latest defer in the chain" );
300+
strictEqual( value, 6, "proper value received" );
301+
});
302+
});
303+
304+
278305
test( "jQuery.when" , function() {
279306

280307
expect( 23 );

0 commit comments

Comments
 (0)