🌐 AI搜索 & 代理 主页
Skip to content

Commit 183f37e

Browse files
committed
jquery ajax: closes #4994. Adding 'context' setting to $.ajax
1 parent c4f144e commit 183f37e

File tree

2 files changed

+60
-17
lines changed

2 files changed

+60
-17
lines changed

src/ajax.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

test/unit/ajax.js

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,44 @@ test("jQuery.ajax() - error callbacks", function() {
7070
});
7171
});
7272

73+
test("Ajax events with context", function() {
74+
expect(6);
75+
76+
stop();
77+
var context = {};
78+
79+
function event(e){
80+
equals( this, context, e.type );
81+
}
82+
83+
function callback(){
84+
equals( this, context, "context is preserved on callback" );
85+
}
86+
87+
jQuery('#foo').add(context)
88+
.ajaxSend(event)
89+
.ajaxComplete(event)
90+
.ajaxError(event)
91+
.ajaxSuccess(event);
92+
93+
jQuery.ajax({
94+
url: url("data/name.html"),
95+
beforeSend: callback,
96+
success: callback,
97+
error: callback,
98+
complete:function(){
99+
callback.call(this);
100+
setTimeout(proceed, 300);
101+
},
102+
context:context
103+
});
104+
105+
function proceed(){
106+
jQuery('#foo').add(context).unbind();
107+
start();
108+
}
109+
});
110+
73111
test("jQuery.ajax() - disabled globals", function() {
74112
expect( 3 );
75113
stop();
@@ -284,19 +322,20 @@ test("pass-through request object", function() {
284322

285323
test("ajax cache", function () {
286324
expect(18);
325+
287326
stop();
288327

289328
var count = 0;
290329

291330
jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
292331
var re = /_=(.*?)(&|$)/g;
293-
var oldOne = null;
332+
var oldOne = null;
294333
for (var i = 0; i < 6; i++) {
295-
var ret = re.exec(s.url);
334+
var ret = re.exec(s.url);
296335
if (!ret) {
297336
break;
298337
}
299-
oldOne = ret[1];
338+
oldOne = ret[1];
300339
}
301340
equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
302341
ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");

0 commit comments

Comments
 (0)