Bug Tracker

Opened 11 years ago

Closed 11 years ago

Last modified 8 years ago

#3243 closed bug (invalid)

jQuery.ajaxSetup({}) settings are overriden by jQuery.ajax

Reported by: rene7705 Owned by:
Priority: major Milestone: 1.3
Component: ajax Version: 1.2.3
Keywords: ajaxSetup, override Cc: rene7705, flesler
Blocked by: Blocking:

Description

Hi.

For my CMS, i want a statusbar that goes through the following stages:

1) Getting <url>

2) Processing <url>

3) Processed <url>

I want to achieve this by setting some global handlers for ajax events. I have tried using the global events, but they never got fired.

So at document-ready, i do this:

jQuery.ajaxSetup ({

type : "GET", dataType : "html", beforeSend : function (xhr) {

if (mb.desktop.isAnAjaxActivityToReport(this)) {

mb.reportToEndUser("Getting " + this.url, true);

}; if (typeof jsDump!='undefined') mb.reportToDebugger ('jQuery.ajax: about to send:\n'+jsDump.parse(this.dataPosted));

}, success : function (data, ts){

if (mb.desktop.isAnAjaxActivityToReport(this)) {

mb.reportToEndUser("Processing " + this.url, true);

};

}, complete : function (xhr, ts) {

if (mb.desktop.isAnAjaxActivityToReport(this)) {

mb.reportToEndUser("Processed " + this.url, false);

};

}

});

But this wont work because local callbacks in jQuery.ajax() overrides any global callbacks with the same name made by jQuery.ajaxSetup()

I think it's also wise to fire global handlers Before local handlers. The global handler updates the status-bar, and the local handler does the actual work. So I need the global handler to fire Before the local handler.

The simple solution is to change jquery-1.2.3.source.js around line 2781 to this:

function success(){

Fire the global callback

if (jQuery.ajaxSettings.success)

jQuery.ajaxSettings.success.call (s, data, status);

If a local callback was specified, fire it and pass it the data

if ( s.success )

s.success( data, status );

Fire the global callback

if ( s.global )

jQuery.event.trigger( "ajaxSuccess", [xml, s] );

}

function complete(){

Fire the global callback

if (jQuery.ajaxSettings.complete)

jQuery.ajaxSettings.complete.call (s, xml, status);

Process result

if ( s.complete )

s.complete(xml, status);

The request was completed

if ( s.global )

jQuery.event.trigger( "ajaxComplete", [xml, s] );

Handle the global AJAX counter

if ( s.global && ! --jQuery.active )

jQuery.event.trigger( "ajaxStop" );

}

I'd like to know if i'm on the right track here, and hope this solution can be incorporated in future jquery releases. Please mail-CC rene7705@… if you have some kind of answer to this ticket.

Change History (11)

comment:1 Changed 11 years ago by flesler

Cc: rene7705 flesler added
Milestone: 1.2.31.3
need: ReviewTest Case

Global events do work, the option 'global' is true by default. About AjaxSetup... that's the expected behavior, the function sets the defaults for ajax requests.

Can you provide a test case where we can reproduce the initial problem with the global events ?

comment:2 Changed 11 years ago by rene7705

Global events are fired _after_ the local event finishes. Wouldn't it make more sense to fire them before the local event is triggered?

Currently i dont use the global events. Please provide me with a correct example so i can hook those events.

comment:3 Changed 11 years ago by flesler

What do you consider global and local events ?

comment:4 Changed 11 years ago by rene7705

global = set by jQuery("#something").ajaxSuccess local = set in jQuery.ajax({}) according to the docs ;)

comment:5 Changed 11 years ago by flesler

Ok so... In short, you don't have a problem with global events as you stated in the description, right ? Just the order of local vs global events ?

comment:6 Changed 11 years ago by rene7705

I can't get global events to work at all.

And the order of calls is significant, yes.

comment:7 Changed 11 years ago by flesler

And the order of calls is significant, yes.

Ok.. if you think the order should be reversed, create a new feature ticket. Include a situation where the new order is clearly better than the actual one.

I can't get global events to work at all.

This ticket will only handle the bug you mention. You need to provide a test case where they don't work. If you simply don't know how to use them properly, then check the docs or ask on the google group.

comment:8 Changed 11 years ago by rene7705

I do:

$('#mbStatusbar').ajaxSuccess(function(evt, request, settings){

alert (1);

$(this).html("<li>Successful Request!</li>");

});

and the event never fires. i took this snippet straight from the docs.

I can't provide a test-url at the moment because it's located on a development server behind a firewall.

comment:9 Changed 11 years ago by rene7705

note that #mbStatusbar does exist, i checked. i refer to it case-sensitive.

comment:10 Changed 11 years ago by rene7705

OK, my bad :-/

I was calling .ajaxSuccess before my page had initialized..

think i can get my statusbar to work with the event-structure as it is. this ticket can be closed if you want; i realize that changing the .ajaxSetup structure might well result in broken apps for people who already use the existing structure.

comment:11 Changed 11 years ago by flesler

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.