Use jQuery.when() in a widget without losing callback context?

Use jQuery.when() in a widget without losing callback context?

I'm struggling with using $.when() inside a Widget Factory widget. The widget needs to make multiple AJAX operations, then trigger a single callback. The callback is one of the widget's public methods.

I do something like:

  1. var promises = [];

  2. if (test) {
  3.       promises.push( $.ajax({...} );
  4. }

  5. if (test) {
  6.       promises.push( $.ajax({...} );
  7. }

  8. if (promises.length) {
  9.       $.when.apply(this, promises).done(this.widget_method);
  10. }

My problem is that the callback's context isn't the widget instance like it would normally be. When the callback runs, `this` is an Array of what appear to be objects from each AJAX call, and two additional properties _super and _superApply. Those properties are functions, but they don't work. I tried calling _super() and got an exception from jQuery.

How do I use when() inside a widget method and maintain the widget object as context?