2

I'd like to load some JSON data from an external service. However, it provides

{ foo: ..., bar: ..., useful: {...} }

and what I really care about is in the "useful" part. I need to pass just that part to the success callback.

I'm trying to use Deferred to load from multiple data sources at once -- something like this. I'd like to retrieve the data, "massage" the result, and have just the "useful" part in the example above actually passed to the then callback. My understanding is that when you pass a Deferred to when(), the data goes directly to the callback passed to then(), so I need to hook into the process before it gets there.

I tried dataFilter but for JSONP that's not possible. Is there any other way to intercept these results? I can add some checks to my then() callback to handle cached data differently from the "fresh" results, but that sort of loses the magic of Deferred in the first place.

To clarify, this doesn't work:

$.when($.ajax({
  url: "host/service",
  dataType: "jsonp",
  dataFilter: function(data, type){
    return data.useful; // throws, data === undefined
  }
})).then(function(usefulStuff){ ... });
Community
  • 1
  • 1
Coderer
  • 25,844
  • 28
  • 99
  • 154

1 Answers1

2

You can call .pipe() to process the data and create a new Deferred:

$.getJSON(...).pipe(function(results) {
    return ...;
})
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964