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

feat($resource): add support for wrapped responses #6138

Open
stephenleicht opened this issue Feb 5, 2014 · 25 comments
Open

feat($resource): add support for wrapped responses #6138

stephenleicht opened this issue Feb 5, 2014 · 25 comments

Comments

@stephenleicht
Copy link

$resource cannot handle a response that has the data you want wrapped in a object that has additional metadata.

for example if the response from the server is:

{
    "meta": {
        "total_count": 100
    },
    "items": [/*Some sort of list of objects*/]
}

You lose a lot of the cool $resource features because your resource is actually the 'items' field and not the entire response.

transformRepsponse does not work for this because if you pull out the 'items' field there you lose the meta field data.

response interceptor doesn't work because by the time you get it has already been decorated as a Resource object.

there should probably be an additional field in the action configuration to specify what field in the response should be decorated as a Resource.

stephenleicht pushed a commit to stephenleicht/angular.js that referenced this issue Feb 5, 2014
add a resourceData field on the action configuration. This allows you to specify which field in the response should be turned into the resource object instead of the whole thing always being decorated. You can use a string to use just a single simple field or a function for more advanced control

Closes angular#6138
@stephenleicht
Copy link
Author

I don't know why, but it created a new thing when I submitted the pull request. #6139

@stephenleicht stephenleicht reopened this Feb 5, 2014
@btford btford added this to the 1.3.x milestone Feb 6, 2014
@tbosch tbosch self-assigned this Feb 6, 2014
@tbosch
Copy link
Contributor

tbosch commented Feb 6, 2014

What are you doing with the meta data when you only would like to have the items in the resource?

@tbosch tbosch modified the milestones: Purgatory, 1.3.x Feb 6, 2014
@tbosch
Copy link
Contributor

tbosch commented Feb 6, 2014

Could you create a jsfiddle / plunker for this also?

@stephenleicht
Copy link
Author

I'm not entirely sure how to create a jsfiddle for it as the feature doesn't exist, but I can try.

The best example for needing the data to be Resources and the metadata to be still intact is paging.

Take the example above where meta has a "total_count" property, possibly other information about the request you made. You need that information to tell the user how many pages there are and such. If you can tell $resource to use just the items array, you get all the nice functions like $save and the like on the individual items in the array like you would if the call returned just an array.

@tbosch
Copy link
Contributor

tbosch commented Feb 6, 2014

Still don't get it where/how in your code you are extracting the meta data, given your PR. Could you paste some code?

@stephenleicht
Copy link
Author

Oh of course. I'm using an interceptor on the action. I'll admit it's a bit wonky, but it's the best way I came up with with the most minor code changes. In most cases on an API like this you can throw the metadata away and not particularly care. Pagination is a fairly common exception though.

interceptor: {
            response: function(response)
            {
                return {meta: response.data.meta, data: data.resource};
            }}

That way when the object is finally returned you have the meta field intact and the data field has been converted to a Resource.

@stephenleicht
Copy link
Author

Here is the plunker you asked for: http://plnkr.co/edit/k2hsvE3eTQnhWhzWIda8?p=preview

I'm using the modified version of angular-resource from the pull request.

@caitp
Copy link
Contributor

caitp commented Feb 6, 2014

I like the tests, but I feel like this interface is super clunky.

@stephenleicht
Copy link
Author

You're absolutely right. I didn't really like it either. I took a step back and thought about it again and came up with another (cleaner) way. See http://plnkr.co/edit/mv5cSEyNQJBwpKfdPXZz?p=preview

When specifying which field should be turned into the resource, the full response from the server will still be returned, but with only the field specified decorated as a Resource. No need for the interceptor.

stephenleicht pushed a commit to stephenleicht/angular.js that referenced this issue Feb 6, 2014
add a resourceData field on the action configuration. This allows you to specify which field in the response should be turned into the resource object instead of the whole thing always being decorated. You can use a string to use just a single simple field or a function for more advanced control

Closes angular#6138
@tbosch tbosch removed their assignment Feb 6, 2014
@scriby
Copy link

scriby commented Aug 8, 2014

+1

2 similar comments
@hippich
Copy link

hippich commented Aug 8, 2014

+1

@mindywhitsitt
Copy link

+1

@btford btford removed the gh: issue label Aug 20, 2014
@ghost
Copy link

ghost commented Oct 8, 2014

Well since this is the only feature that keeps me from using the standard $resource that comes with angular (I'm using an fork that has this feature), I have to upvote this... Please, add this to angular-resource!

Also, this is very usefull, since almost all rest API's (including the own github api), wrap the response in a object.

+1

@iyeldinov
Copy link

👍

2 similar comments
@uamanager
Copy link

+1

@andrewdacenko
Copy link

+1

@detonavomek-zz
Copy link

+0.8

@Pro100Fant
Copy link

+1

@mkoryak
Copy link

mkoryak commented Nov 12, 2014

@cinnamon25 what fork is that? Id like to use it.

furthermore,
as someone just starting out with angular, it seems crazy to me that this functionality isnt part of angular. How do people handle pagination, how to people handle errors or messages that can come bundled with data?
I had this problem 2 years ago when I first tried learning angular, and since it still exists, methinks that I am doing it wrong.

+2

@iyeldinov
Copy link

@mkoryak, here is a good workaround http://jsfiddle.net/wortzwei/bez79/

@mkoryak
Copy link

mkoryak commented Nov 12, 2014

@Iyel Thank you! This is exactly what I was trying to write myself but got burned by that internal copy operation that the interceptor fixes.

@Farson
Copy link

Farson commented Jun 19, 2015

+1

@kavi87
Copy link

kavi87 commented Jun 23, 2015

+1 This feat is crucial if you want to use an hypermedia format such as HAL.

@andreykl
Copy link

+1

@gkalpak gkalpak modified the milestones: Ice Box, Purgatory May 31, 2016
@gkalpak
Copy link
Member

gkalpak commented May 31, 2016

I don't think there is a clean way to support this.
If anyone absolutely needs to, they can always use the more low-level $http.

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

Successfully merging a pull request may close this issue.