3

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?

1 Answer 1

2

'@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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.