angularjs - What is a proper way to write an interceptor for the resource? -


i have need transform response service on each get, save, update. i've created resource , added transformer gets executed, structure of object being returned not same when don't use transformer. here talking structure of response, not object transforming. here resource:

angular.module('app')    .factory('insureds', ['$resource', 'config',  function ($resource, config) {         function transform(response) {             var insured = response.data.insured;              return response;        }         var memberserviceshostname = config.memberserviceshostname;        return $resource(memberserviceshostname + '/insureds/:insuredid', null,        {            'get': {                method: 'get', 'withcredentials': true, interceptor:                {                     response: function (response) { return transform(response).data; }                }            },            'update': { method: 'put', 'withcredentials': true },            'save': { method: 'post', 'withcredentials': true }        });    }]); 

when don't use transformer "insured" on first level when promise gets resolved resolves instance of insured object. transformer there wrapper object, contains insured , responsestatus properties. has returning "reponse" in interceptor. should 1 return, original response, doing, or response.data, or response.resource.insured? confused...

the default response interceptor this:

function defaultresponseinterceptor(response) {   return response.resource; } 

therefore, if preserve default behaviour, have return response.resource instead of response.data:

return $resource(memberserviceshostname + '/insureds/:insuredid', null, {   get: {     method: 'get',     withcredentials: true,     interceptor: {       response: function (response) {         return transform(response).resource;       }     }   },   ... 

hope helps.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -