DISABLE ADBLOCK

ADBlock is blocking some content on the site

ADBlock errore

angular - how to get resource parameter from custom transformResponse

StackOverflow https://stackoverflow.com/questions/20301779
 Checked

Question

is it possible to get resource param from transformResponse function?

demoApp.factory('Social', function($resource) {
  return $resource('api/social/:platform', {
    platform: '@platform'
  }, {
    getAdmin: {
      method: 'GET',
      isArray: false,
      transformResponse: function(resp) {
        // so, can i get `platform` in line 3 from here?
        return resp[platform];
      }
    }
  });
});

as shown in the code above, can i get the parameter platform from the transformResponse function?

Solution

'@platform' as a parameter means that you will be 'GET'ting the url 'api/social/', and putting the value of the returned objects 'platform' parameter into storage.

When calling the remaining non-get functions of the resource, (by default 'save', 'remove', 'delete'), the $resource will modify the url by replacing ':platform' with the previously saved value.

So, since you are in the get request, you can obtain the parameter exactly as you've specified.

transformResponse: function(resp)
{
    // so, can i get `platform` in line 3 from here?
    console.log(resp.platform); // Yes you can
    return resp[platform];
}

HOWEVER, since the data being returned is now resp.platform, the url parameter is being set to resp.platform.platform... Probably not what you want, and I'm guessing the source of what's not working for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow