Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$resource transformResponse not working

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.

like image 864
Caio Cunha Avatar asked Mar 16 '13 22:03

Caio Cunha


2 Answers

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.

like image 169
rgaskill Avatar answered Nov 14 '22 21:11

rgaskill


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.
like image 34
Stewie Avatar answered Nov 14 '22 23:11

Stewie