Bug Tracker

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#11749 closed enhancement (fixed)

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:


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()

Change History (10)

comment:1 Changed 5 years ago by dmethvin

Owner: set to gravof
Status: newpending

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

comment:2 Changed 5 years ago by gravof

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.

comment:3 Changed 5 years ago by dmethvin

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.

comment:4 Changed 5 years ago by gibson042

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).

comment:5 Changed 5 years ago by dmethvin

Cc: jaubourg added

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().

comment:6 Changed 5 years ago by jaubourg

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.

comment:7 Changed 5 years ago by jaubourg

Component: unfileddeferred
Milestone: None1.8
Owner: changed from gravof to jaubourg
Priority: undecidedlow
Status: newassigned

comment:8 Changed 5 years ago by jaubourg

Resolution: fixed
Status: assignedclosed

comment:9 Changed 5 years ago by jaubourg

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

comment:10 Changed 5 years ago by jaubourg

#11476 is a duplicate of this ticket.

Note: See TracTickets for help on using tickets.