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:
- var promises = [];
- if (test) {
- promises.push( $.ajax({...} );
- }
- if (test) {
- promises.push( $.ajax({...} );
- }
- if (promises.length) {
- $.when.apply(this, promises).done(this.widget_method);
- }
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?