]> source.dussan.org Git - jquery.git/commitdiff
Propagete context of returned deferred object in Deferred.then(). Fixes #13160
authornanto_vi <nanto@moon.email.ne.jp>
Sun, 6 Jan 2013 08:09:28 +0000 (17:09 +0900)
committernanto_vi <nanto@moon.email.ne.jp>
Sun, 6 Jan 2013 17:49:15 +0000 (02:49 +0900)
src/deferred.js
test/unit/deferred.js

index d842065704e201d41a608f91934364d1a4d2f74c..4001c2384540f22513020a4d8aeab0b944f99aea 100644 (file)
@@ -35,7 +35,9 @@ jQuery.extend({
                                                                                newDefer[ action + "With" ]( this === promise ? newDefer.promise() : this, [ returned ] );
                                                                        }
                                                                } :
-                                                               newDefer[ action ]
+                                                               function() {
+                                                                       newDefer[ action + "With" ]( this === promise ? newDefer.promise() : this, arguments );
+                                                               }
                                                        );
                                                });
                                                fns = null;
@@ -72,7 +74,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;
index 86b0b34116a0344cee113656662fec4551b4be85..f9bc13a9c68bcdf73c65d9eb0896727f8cf5be01 100644 (file)
@@ -273,7 +273,7 @@ test( "jQuery.Deferred.then - deferred (progress)", function() {
 
 test( "jQuery.Deferred.then - context", function() {
 
-       expect( 4 );
+       expect( 7 );
 
        var context = {};
 
@@ -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;
@@ -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() {