Pregunta

Below is my code

MY_MODULE.factory("SOME_API", ['$resource', '$http', '$rootScope', function($resource, $http, $rootScope){

  return $resource("/appi/get/", {responseFormat: 'json', responselanguage:'English', pageno:1}, {
    search:{
      method:'GET',
      transformResponse: [function (data, headersGetter) {
          console.log(data);
          // you can examine the raw response in here
          $log.info(data);
          $log.info(headersGetter());
          return {tada:"Check your console"};
      }].concat($http.defaults.transformResponse),
      params:{
        param1:'SOME_DATA',
        param2:'SOME_DATA2',
      }
    }
  }
}

I m using angular 1.0.7, can't figure out why my transformResponse is not called. I believe this version of angular supports transformResponse, if not how to implement similar callback.

¿Fue útil?

Solución 3

Below is the solution, basically a wrapper to $http, which emulates default resource

$rootScope.woiresource = function (url, defaults, actions) {
  var $res = {};

  for (var i in actions) {

    var default_params = {};

    for (var di in defaults) {
      default_params[di] = defaults[di];
    }

    for (var ai in actions[i].params) {
      default_params[ai] = actions[i].params[ai];
    }

    $res[i] = (function (url, method, default_params) {
      return function (params, callback, headers) {

        if (typeof params == 'function') {
          callback = params;
          params = {};
        }

        if (typeof headers == 'undefined') {
          headers = {};
        }

        for (var pi in params) {
          default_params[pi] = params[pi];
        }

        return $http({
          url: url,
          method: method,
          transformResponse: [function (strData, headersGetter) {
              //Do Something

          }].concat($http.defaults.transformResponse),
          params: default_params,
          headers: headers,

        }).success(function (data) {
          callback(data);
        });
      };
    })(url, actions[i].method, default_params);
  }

  return $res;
};

Parameters are similar to default resource just changed the working, hope this helps some people.

Otros consejos

transformResponse is not a parameter to $resource. The response interceptor are configured on $httpProvider. See the documentation of $http (Section Response Interceptor).

Keep in mind that once configured these interceptors would run for each request that was made by $http.

Is it possible to use AngularJS 1.2?

I had the exact same problem with 1.0.8. After I changed to angular-1.2.0-rc.2, my resource's transformResponse callback started being called.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top