Frage

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?

War es hilfreich?

Lösung

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top