5

Got a simplified $resource example here (adapted from Angular site):

angular.module('project', ['mongolab']);

function ListCtrl($scope, Project) {
  $scope.projects = Project.test();
}

angular.module('mongolab', ['ngResource']).
factory('Project', function ($resource) {
  var url, dfParams, actions;

  url = 'https://api.mongolab.com/api/1/databases' + '/angularjs/collections/projects/:id';
  dfParams = {
    apiKey: '4f847ad3e4b08a2eed5f3b54'
  };

  actions = {
    test: {
      method: 'GET',
      isArray: true,
      transformResponse: function (response) {
        // line is never getting called
        console.log('transforming');
        return response;
      }
  };

  var Project = $resource(url, dfParams, actions);
  return Project;
});

The question is that the line console.log('transforming') is never getting called. Why is that? Everything else works fine.

Live fiddle here.

joragupra
  • 692
  • 1
  • 12
  • 23
Caio Cunha
  • 23,326
  • 6
  • 78
  • 74

3 Answers3

10

This functionality is only available in the 1.1.2 or later versions of AngularJs. It is not available in the 1.1.1 or earlier versions of AngularJs.

rgaskill
  • 2,067
  • 1
  • 14
  • 11
2

Response transformation callback is not exposed in $resource service. It lives inside the underlying $http service.

So you have two choices:

  • Use "low-level" $http instead of $resource abstraction,
  • Create some kind of wrapper around your resource, which would transform the response the way you want.
Stewie
  • 60,366
  • 20
  • 146
  • 113
  • I thought that too (not exposed in `$resource`). But then I looked at `$resource`'s code and it's documented in the [source code](https://github.com/angular/angular.js/blob/master/src/ngResource/resource.js#L69-L74). Also, reading through the code, you see it extends the `httpConfig` and [passes to underlying `$http`](https://github.com/angular/angular.js/blob/master/src/ngResource/resource.js#L437-L447). And it seems to be intentional, as they [wrote a test case](https://github.com/angular/angular.js/blob/master/test/ngResource/resourceSpec.js#L686-L705) for it. – Caio Cunha Mar 16 '13 at 23:05
  • 1
    You were looking at unstable master branch, which isn't yet released. – Stewie Mar 17 '13 at 08:51
  • Yes, I got it by @rgaskill answer's. Thanks. – Caio Cunha Mar 17 '13 at 12:45
1

I have the same issue,and I use AngularJS 1.2.26.
I have defined a interceptor that contains a transformResponse of $http level like:

return {
  'request': function (config) {
      config.defaults.transformResponse = function(data){
          //some has been done
      };
      return config;
},

when I DELETE the code above, my transformResponse in $resource worked!
In order to solve this problem,I defined my transformResponse in $resource like:

return $resource(pathConfig.serverURL + ':demoId',
    {
        demoId: "@id"
    },
    {
        hello: {
            method: "GET",
            url: serverDomain + ":demoId",
            transformResponse: addTransformResponses(
                [
                    function (data, getHeaders) {
                        //doing hello's OWN interceptors
                    }
                ])
        }
    });

and addTransformResponses contains common interceptor like:

addTransformResponses: function (addTrans) {
    return [this.parseResponseJson, this.parseResponseArray]
        .concat(addTrans);
},

//parse JSON
parseResponseJson: function (data, getHeaders) {
    try {
        return angular.fromJson(data);
    } catch (e) {
        console && console.error("return data is not a JSON!");
        return data;
    }
},
parseResponseArray: function (data, getHeaders) {
    if (angular.isArray(data)) {
        return {"array": data};
    }
    return data;
}

by doing this, you can configure common interceptors and some request's own interceptors well! :)
If any practice better,please tell me! thanks!

soytian
  • 318
  • 2
  • 3
  • Where exactly are you adding the `addTransformResponses`, `parseResponseJson` and `parseResponseArray` functions? – Mauro May 05 '15 at 15:05