Bug Tracker

Ticket #11749 (closed enhancement: fixed)

Opened 2 years ago

Last modified 2 years ago

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
Blocking: Blocked by:

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

Change History

comment:1 Changed 2 years ago by dmethvin

  • Owner set to gravof
  • Status changed from new to pending

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

comment:2 Changed 2 years ago by gravof

  • Status changed from pending to new

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 2 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 2 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 2 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 2 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 2 years ago by jaubourg

  • Owner changed from gravof to jaubourg
  • Priority changed from undecided to low
  • Status changed from new to assigned
  • Component changed from unfiled to deferred
  • Milestone changed from None to 1.8

comment:8 Changed 2 years ago by jaubourg

  • Status changed from assigned to closed
  • Resolution set to fixed

comment:9 Changed 2 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 2 years ago by jaubourg

#11476 is a duplicate of this ticket.

Note: See TracTickets for help on using tickets.