Skip to main content

Bug Tracker

Side navigation

#12924 closed bug (notabug)

Opened November 20, 2012 07:39AM UTC

Closed November 22, 2012 03:57PM UTC

Last modified November 23, 2012 06:03AM UTC

ajaxComplete does not fire if context is detached in success handler

Reported by: sac@crasman.fi 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;
}
Attachments (0)
Change History (4)

Changed November 20, 2012 08:16AM UTC by sac@crasman.fi comment:1

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

Changed November 20, 2012 03:38PM UTC by dmethvin comment:2

cc: → jaubourg

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?

Changed November 22, 2012 03:57PM UTC by jaubourg comment:3

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.

Changed November 23, 2012 06:03AM UTC by sac@crasman.fi comment:4

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.