@@ -41,21 +41,20 @@ jQuery.fn.extend({
4141 }
4242 }
4343
44- var self = this ;
45-
4644 // Request the remote document
4745 jQuery . ajax ( {
4846 url : url ,
4947 type : type ,
5048 dataType : "html" ,
5149 data : params ,
50+ context :this ,
5251 complete : function ( res , status ) {
5352 // If successful, inject the HTML into all the matched elements
5453 if ( status === "success" || status === "notmodified" ) {
5554 // See if a selector was specified
56- self . html ( selector ?
55+ this . html ( selector ?
5756 // Create a dummy div to hold the results
58- jQuery ( "<div/>" )
57+ jQuery ( "<div />" )
5958 // inject the contents of the document in, removing the scripts
6059 // to avoid any 'Permission Denied' errors in IE
6160 . append ( res . responseText . replace ( rscript , "" ) )
@@ -68,7 +67,7 @@ jQuery.fn.extend({
6867 }
6968
7069 if ( callback ) {
71- self . each ( callback , [ res . responseText , status , res ] ) ;
70+ this . each ( callback , [ res . responseText , status , res ] ) ;
7271 }
7372 }
7473 } ) ;
@@ -193,8 +192,9 @@ jQuery.extend({
193192 // Extend the settings, but re-extend 's' so that it can be
194193 // checked again later (in the test suite, specifically)
195194 s = jQuery . extend ( true , s , jQuery . extend ( true , { } , jQuery . ajaxSettings , s ) ) ;
196-
195+
197196 var jsonp , status , data ,
197+ callbackContext = s . context || window ,
198198 type = s . type . toUpperCase ( ) ;
199199
200200 // convert data if not already a string
@@ -352,7 +352,7 @@ jQuery.extend({
352352 } catch ( e ) { }
353353
354354 // Allow custom headers/mimetypes and early abort
355- if ( s . beforeSend && s . beforeSend ( xhr , s ) === false ) {
355+ if ( s . beforeSend && s . beforeSend . call ( callbackContext , xhr , s ) === false ) {
356356 // Handle the global AJAX counter
357357 if ( s . global && ! -- jQuery . active ) {
358358 jQuery . event . trigger ( "ajaxStop" ) ;
@@ -364,7 +364,7 @@ jQuery.extend({
364364 }
365365
366366 if ( s . global ) {
367- jQuery . event . trigger ( "ajaxSend" , [ xhr , s ] ) ;
367+ trigger ( "ajaxSend" , [ xhr , s ] ) ;
368368 }
369369
370370 // Wait for a response to come back
@@ -464,31 +464,35 @@ jQuery.extend({
464464 function success ( ) {
465465 // If a local callback was specified, fire it and pass it the data
466466 if ( s . success ) {
467- s . success ( data , status ) ;
467+ s . success . call ( callbackContext , data , status ) ;
468468 }
469469
470470 // Fire the global callback
471471 if ( s . global ) {
472- jQuery . event . trigger ( "ajaxSuccess" , [ xhr , s ] ) ;
472+ trigger ( "ajaxSuccess" , [ xhr , s ] ) ;
473473 }
474474 }
475475
476476 function complete ( ) {
477477 // Process result
478478 if ( s . complete ) {
479- s . complete ( xhr , status ) ;
479+ s . complete . call ( callbackContext , xhr , status ) ;
480480 }
481481
482482 // The request was completed
483483 if ( s . global ) {
484- jQuery . event . trigger ( "ajaxComplete" , [ xhr , s ] ) ;
484+ trigger ( "ajaxComplete" , [ xhr , s ] ) ;
485485 }
486486
487487 // Handle the global AJAX counter
488488 if ( s . global && ! -- jQuery . active ) {
489489 jQuery . event . trigger ( "ajaxStop" ) ;
490490 }
491491 }
492+
493+ function trigger ( type , args ) {
494+ ( s . context ? jQuery ( s . context ) : jQuery . event ) . trigger ( type , args ) ;
495+ }
492496
493497 // return XMLHttpRequest to allow aborting the request etc.
494498 return xhr ;
@@ -497,12 +501,12 @@ jQuery.extend({
497501 handleError : function ( s , xhr , status , e ) {
498502 // If a local callback was specified, fire it
499503 if ( s . error ) {
500- s . error ( xhr , status , e ) ;
504+ s . error . call ( s . context || window , xhr , status , e ) ;
501505 }
502506
503507 // Fire the global callback
504508 if ( s . global ) {
505- jQuery . event . trigger ( "ajaxError" , [ xhr , s , e ] ) ;
509+ ( s . context ? jQuery ( s . context ) : jQuery . event ) . trigger ( "ajaxError" , [ xhr , s , e ] ) ;
506510 }
507511 } ,
508512
0 commit comments