pbojinov
4/30/2013 - 5:10 AM

Restangular responseInterceptor

Restangular responseInterceptor

app.config(function(RestangularProvider) {
    // First let's set listTypeIsArray to false, as we have the array wrapped in some other object.
    RestangularProvider.setListTypeIsArray(false);

    // Now let's configure the response extractor for each request
    RestangularProvider.setResponseExtractor(function(response, operation, what, url) {
      var newResponse;
      // This is a get for a list
      if (operation === "getList") {
        // First the newResponse will be response.objects which is actually an array
        newResponse = response.objects;
        // Then we add to this array a special property containing the metadata for paging for example
        newResponse.metadata = response.data.meta;
      } else {
        // If it's an element, then we just return the "regular" response as there's no object wrapping it
        newResponse = response;
      }
      return newResponse;
    });
});