Skip to main content

Bug Tracker

Side navigation

#11476 closed bug (duplicate)

Opened March 15, 2012 04:28PM UTC

Closed July 13, 2012 07:35AM UTC

Last modified July 13, 2012 07:36AM UTC

Ajax context is lost in deferred

Reported by: thanatica Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.7.2rc1
Keywords: Cc:
Blocked by: Blocking:
Description

When using $.ajax as a deferred, the context is lost when the deferred succeeds and does its callback. Consider the following code:

var context = { foo: "bar" };
$.when($.ajax({ url: "...", context: context })).then(function(args) {
  //Where is context gone?
});

At the point of the comment, there is no trace of the original context object. It seems lost. According to the documentation, it should be in args[0], but args is holding [ returned data, "success", jqXHR ].

Now of course, in this example. it's still accessible due to its scope, but the whole point is to keep it accessible even if it gets out of scope.

Attachments (0)
Change History (6)

Changed March 22, 2012 01:23PM UTC by jaubourg comment:1

description: When using $.ajax as a deferred, the context is lost when the deferred succeeds and does its callback. Consider the following code: \ \ var context = { foo: "bar" }; \ $.when($.ajax({ url: "...", context: context })).then(function(args) { \ //Where is context gone? \ }); \ \ At the point of the comment, there is no trace of the original context object. It seems lost. According to the documentation, it should be in args[0], but args is holding [ returned data, "success", jqXHR ]. \ \ Now of course, in this example. it's still accessible due to its scope, but the whole point is to keep it accessible even if it gets out of scope.When using $.ajax as a deferred, the context is lost when the deferred succeeds and does its callback. Consider the following code: \ {{{ \ var context = { foo: "bar" }; \ $.when($.ajax({ url: "...", context: context })).then(function(args) { \ //Where is context gone? \ }); \ }}} \ At the point of the comment, there is no trace of the original context object. It seems lost. According to the documentation, it should be in args[0], but args is holding [ returned data, "success", jqXHR ]. \ \ Now of course, in this example. it's still accessible due to its scope, but the whole point is to keep it accessible even if it gets out of scope.
resolution: → worksforme
status: newclosed

Changed July 13, 2012 03:32AM UTC by james comment:2

for one argument, but not for more than one.

http://jsfiddle.net/E5rqx/1/

Changed July 13, 2012 04:19AM UTC by rwaldron comment:3

You can only ever have one context, so why would even do that to begin with? I agree that it shouldn't be "undefined", but why would you jump through all of that code? If you want to set an explicit context object, use $.proxy:

http://jsfiddle.net/rwaldron/E5rqx/3/

Changed July 13, 2012 07:35AM UTC by jaubourg comment:4

resolution: worksforme
status: closedreopened

Changed July 13, 2012 07:35AM UTC by jaubourg comment:5

resolution: → duplicate
status: reopenedclosed

Changed July 13, 2012 07:36AM UTC by jaubourg comment:6

Duplicate of #11749.