]> source.dussan.org Git - jquery.git/commitdiff
Ajax: execute jQuery#load callback with correct context
authorOleg Gaidarenko <markelog@gmail.com>
Mon, 4 Apr 2016 19:42:01 +0000 (22:42 +0300)
committerTimmy Willison <timmywillisn@gmail.com>
Mon, 4 Apr 2016 20:22:35 +0000 (16:22 -0400)
Thanks @blq (Fredrik Blomqvist)

Fixes gh-3035
Close gh-3039

src/ajax/load.js
test/unit/ajax.js

index 82f0cf308086e6e4f4c5654f5ef8b5d5c9ea1022..aa6e1cfab143df52fc095d308bdab68dba1a6be3 100644 (file)
@@ -62,7 +62,7 @@ jQuery.fn.load = function( url, params, callback ) {
                // If it fails, this function gets "jqXHR", "status", "error"
                } ).always( callback && function( jqXHR, status ) {
                        self.each( function() {
-                               callback.apply( self, response || [ jqXHR.responseText, status, jqXHR ] );
+                               callback.apply( this, response || [ jqXHR.responseText, status, jqXHR ] );
                        } );
                } );
        }
index 949bf5f20225cc6abe170d5384a4f22af8bd55aa..297f051d9f465f07468a19d9d461b450bd1926c0 100644 (file)
@@ -2084,6 +2084,25 @@ if ( typeof window.ArrayBuffer === "undefined" || typeof new XMLHttpRequest().re
                }
        );
 
+       QUnit.test(
+               "jQuery#load() - should resolve with correct context", 2,
+               function( assert ) {
+                       var done = assert.async();
+                       var ps = jQuery( "<p></p><p></p>" );
+                       var i = 0;
+
+                       ps.appendTo( "#qunit-fixture" );
+
+                       ps.load( "data/ajax/method.php", function() {
+                               assert.strictEqual( this, ps[ i++ ] );
+
+                               if ( i === 2 ) {
+                                       done();
+                               }
+                       } );
+               }
+       );
+
        QUnit.test(
                "#11402 - jQuery.domManip() - script in comments are properly evaluated", 2,
                function( assert ) {