Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Extended customizability of $resource #1045

Closed
wants to merge 1 commit into from
Closed

Extended customizability of $resource #1045

wants to merge 1 commit into from

Conversation

ajoslin
Copy link
Contributor

@ajoslin ajoslin commented Jun 12, 2012

Added an option to $resource's action parameters. The user can define a custom httpConfig and a custom parseResponse function. These allow the user to work with a lot more systems (sending custom headers through action.httpConfig and parsing weird responses with parseResponse).

Example use case. these additions let me use www.parse.com and $resource together easily.

var myHttpConfig = { headers: myHeaders }; //Our pre-defined custom headers to use parse.com
var ParseResource = $resource('https://api.parse.com/1/classes/:class/:id', {
  class: '@class',
  id: '@id',
}, {
  get: { method: 'GET', httpConfig: myHttpConfig },
  create: { method: 'POST', httpConfig: myHttpConfig },
  save: { method: 'PUT', httpConfig: myHttpConfig },
  query: { 
    httpConfig: myHttpConfig,
    isArray: true,
    // Parse.com's response from a query is stored in response.data.result,
    // a parseResponse function allows us to use this
    parseResponse: function(response) {
      return response.data.result;
    }
  }, 
  remove: { method: 'DELETE', httpConfig: myHttpConfig }
});

I don't know if action.httpConfig and action.parseResponse are the best names, but I think the idea of adding these two simple things adds a lot more flexibility to $resource.

@chrisbranson
Copy link

+1 for this

@bsparks
Copy link

bsparks commented Aug 22, 2012

+1

@mhevery
Copy link
Contributor

mhevery commented Sep 5, 2012

Thanks for your contribution! In order for us to be able to accept it, we ask you to sign our CLA (contributor's license agreement).

CLA is important for us to be able to avoid legal troubles down the road.

For individuals (a simple click-through form):
http://code.google.com/legal/individual-cla-v1.0.html

For corporations (print, sign and scan+email, fax or mail):
http://code.google.com/legal/corporate-cla-v1.0.html

@ajoslin
Copy link
Contributor Author

ajoslin commented Sep 6, 2012

ok, submitted the form 👏

@ahall
Copy link

ahall commented Sep 18, 2012

Is this in yet?

@esbullington
Copy link

ahall, this is not in yet, but you can duplicate the parse.com functionality, using the current release, just by changing httpConfig to headers in the example shown above. There's not yet the ability to define a custom httpConfig, but you can use custom headers now.

@benlesh
Copy link
Contributor

benlesh commented Sep 18, 2012

@ajoslin, nice contribution. Very nice. I have use for this in an upcoming project and I was a little concerned about how I'd implement it.

@fx
Copy link

fx commented Sep 27, 2012

+2!

@marknadig
Copy link
Contributor

re: parseResponse - would love your thoughts on https://groups.google.com/forum/?fromgroups=#!topic/angular/Y862h6N9Sxc

@pkozlowski-opensource
Copy link
Member

Hi @ajoslin :-) There was a similar issue opened quite some time ago (#736) to support custom headers for resources. This issue was closed from the commit 9b5656e.

From what I can see yours is more flexible as it allows to pass in the whole httpConfig object and not only headers. On top of this it supports this custom parseResponse function so it still might be worth adding.

In any case it can't be merged like this, it would have to be rebased and you would have to decide how to deal with the existing header-adding functionality. On top of rebasing you would have to add / modify docs. Commit message would have to be amended as well to follow the https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/edit#

I hope that you are still motivated to work on this one :-)

@vizo
Copy link

vizo commented Jan 8, 2013

+1!

@brentpayne
Copy link

+1

1 similar comment
@osimicek
Copy link

+1

@osimicek
Copy link

I would suggest change calling
action.parseResponse(response)
to
action.parseResponse(response, value)
If METADATA is included in response, I can pass it to value.

@mhevery
Copy link
Contributor

mhevery commented Jan 18, 2013

In the current state the PR can not be merged do to:

  1. different way of handling headers from the config.
  2. missing documentation of the feature.

I think the way to go forward is to copy all of the $http.config properties from the resource definition to $http.config
Something like this

var ParseResource = $resource('https://api.parse.com/1/classes/:class/:id', {
  class: '@class',
  id: '@id',
}, {
  get: { method: 'GET', httpConfig: myHttpConfig },
  create: { method: 'POST', httpConfig: myHttpConfig },
  save: { method: 'PUT', httpConfig: myHttpConfig },
  query: { 
    headers: {},
    transformRequest: ,
    transformResponse: ,
    cache: ,
    timeout ,
    withCredentials, 
    isArray: true,
    // Parse.com's response from a query is stored in response.data.result,
    // a parseResponse function allows us to use this
    parseResponse: function(response) {
      return response.data.result;
    }
  }, 
  remove: { method: 'DELETE', httpConfig: myHttpConfig }
});

This would make it consistent with the previous feature set, and be consistent with all of the existing API.

I am closing the issue for now. If you make the changes please reopen it and we will look at it again. Thanks in advance.

@Narretz
Copy link
Contributor

Narretz commented Apr 18, 2013

For those who wonder (or come here from googling), this landed in af89daf (since 1.1.2)
Documentation is in the source file. Note however that setting transformResponse overwrites the regular angular json transformation - you have to do that yourself.

@leefernandes
Copy link
Contributor

Is it possible to set dynamic headers per $resource.action request call yet?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.