-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat($resource): add support for wrapped responses #6138
Comments
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
I don't know why, but it created a new thing when I submitted the pull request. #6139 |
What are you doing with the meta data when you only would like to have the items in the resource? |
Could you create a jsfiddle / plunker for this also? |
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. |
Still don't get it where/how in your code you are extracting the meta data, given your PR. Could you paste some code? |
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. |
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. |
I like the tests, but I feel like this interface is super clunky. |
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. |
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
+1 |
2 similar comments
+1 |
+1 |
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 |
👍 |
2 similar comments
+1 |
+1 |
+0.8 |
+1 |
@cinnamon25 what fork is that? Id like to use it. furthermore, +2 |
@mkoryak, here is a good workaround http://jsfiddle.net/wortzwei/bez79/ |
@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. |
+1 |
+1 This feat is crucial if you want to use an hypermedia format such as HAL. |
+1 |
I don't think there is a clean way to support this. |
$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:
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.
The text was updated successfully, but these errors were encountered: