Bug Tracker

Opened 7 years ago

Closed 6 years ago

Last modified 6 years ago

#12924 closed bug (notabug)

ajaxComplete does not fire if context is detached in success handler

Reported by: sac@… Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.8.3
Keywords: Cc: jaubourg
Blocked by: Blocking:

Description

If the context that is defined for the ajax request is detached in the success handler, the ajaxComplete global handler is never triggered. Or actually it is triggered but since the context is detached from the DOM it is never received by any of the listeners.

I could patch it by adding this to ajax.js in the function done in the last if before the last ajaxComplete trigger. This is probably not the optimal solution but maybe gives an idea what's wrong:

if ( globalEventContext.jquery && !globalEventContext.closest( document.body ).length ) {
	globalEventContext = jQuery.event;
}

Change History (4)

comment:1 Changed 7 years ago by sac@…

I added a demo of the problem at http://jsfiddle.net/ZuUHU/

comment:2 Changed 7 years ago by dmethvin

Cc: jaubourg added

The API docs aren't clear on this at all and I'm not sure they are intuitive. If you provide a context, *some* of the "global" events are fired on that context instead of globally.

One of the changes to be made in 1.9 is to fire these events only on document, but since you're already attaching to there in your sample it should not be impacted by that change.

@jaubourg what do you think?

comment:3 Changed 6 years ago by jaubourg

Resolution: notabug
Status: newclosed

This is really an edge case. It's like expecting elem.click() to bubble up even if elem is detached. If context is detached, then the events are not properly fired and it seems like a perfectly valid behaviour to me. It's not up to $.ajax() to divine that the dev is expecting the event to be fired anyway.

comment:4 Changed 6 years ago by sac@…

From http://api.jquery.com/ajaxComplete/: Whenever an Ajax request completes, jQuery triggers the ajaxComplete event. Any and all handlers that have been registered with the .ajaxComplete() method are executed at this time.

Please change the docs or fix this.

Note: See TracTickets for help on using tickets.