Skip to main content

Bug Tracker

Side navigation

#11749 closed enhancement (fixed)

Opened May 11, 2012 01:45AM UTC

Closed May 12, 2012 07:43PM UTC

Last modified July 13, 2012 07:36AM UTC

Preserve context objects when multiple Deferred object are passed to $.when()

Reported by: gravof Owned by: jaubourg
Priority: low Milestone: 1.8
Component: deferred Version: 1.7.2
Keywords: Cc: jaubourg
Blocked by: Blocking:
Description

A typical use case would be to retrive the url of each ajax request after multiple of them have been passed to $.when()

	$.when($.ajax(url1), $.ajax(url2)).done(function() {
		this[0].url // should equal to url1
		this[1].url // should equal to url2 
	})
	

I'm not sure about this being an array, I just made it up, but I guess we should provide a way to let users access the context object of each Deferred object passed to $.when()

Attachments (0)
Change History (10)

Changed May 11, 2012 01:47AM UTC by dmethvin comment:1

owner: → gravof
status: newpending

Why aren't you using the arguments passed to the .done() function?

Changed May 11, 2012 01:56AM UTC by gravof comment:2

status: pendingnew

Pardon me if I'm wrong, but do these arguments contain the url? The doc didn't say the jqxhr object has an url property, only the context object does.

Changed May 11, 2012 02:02AM UTC by dmethvin comment:3

Oh I see what you're asking for now, the context arg from the ajax options. It doesn't seem right to put that into the this but I agree it doesn't seem to be available at the moment. Perhaps it should be added to the end of the args.

Changed May 11, 2012 02:35AM UTC by gibson042 comment:4

jQuery.when always resolves/rejects with a Promise of its "master" deferred as the context object and the returned values from its subordinate Deferreds as arguments (in the specific case of jQuery.ajax, [ success, statusText, jqXHR ] for success or [ jqXHR, statusText, error ] for failure). We don't expose the context objects, but I could see a case for making the "master" Promise array-like in order to do so, provided we know up front how to handle the special case of jQuery.when(Promise) (where we use the single argument in lieu of making a new Deferred).

Changed May 11, 2012 04:32PM UTC by dmethvin comment:5

cc: → jaubourg

Seems like it would be handy to have the merged-with-$.ajaxSettings options in the jqXHR for cases like this, so they would be passed along with the individual requests.

At this point I suspect we can't fix the shell-game with argument orders across .done(), .fail(), and .always() without serious disruption, but the user can rationalize them with a .pipe().

Changed May 11, 2012 10:07PM UTC by jaubourg comment:6

I actually like this being an array in a when callback. The context has never been documented and is unusable right now... so it shouldn't cause any trouble. The only thing is that I'll have to handle 2 more arrays in when so I have to see if code size won't be an issue.

I don't really like the idea of adding the options object on the jqXHR and I would much prefer a more generic approach in when.

As for the done/fail/always arguments order, we should probably think about it for 1.9.

Changed May 11, 2012 10:07PM UTC by jaubourg comment:7

component: unfileddeferred
milestone: None1.8
owner: gravofjaubourg
priority: undecidedlow
status: newassigned

Changed May 12, 2012 07:43PM UTC by jaubourg comment:8

resolution: → fixed
status: assignedclosed

Changed May 16, 2012 06:05PM UTC by jaubourg comment:9

Preserves context objects when multiple Deferreds are passed to $.when(). Context is an array containing the original contexts in order. When non-observable value is given, associated context is undefined. In case only a single non-observable value is given, context is the global object (thanks so much Function.prototype.apply!). Fixes #11749.

Changeset: f93a2f569d31c4d1fc86ff3ae9605309ac491d68

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

#11476 is a duplicate of this ticket.