0

all my rest API for the query (e.g. /clubs) return an object that is of this form:

{
"total": 10,
"results": [...]
}

the $resourece.query expect an array.

what's the correct way to parse this response?

  • Should I override in the definition of the resource the query setting isArray: false? (if I set the functions to override the query do I've to re-set all the other methods such as get save etc?)
  • Should I create a parseFunction to parse the result and return an array? if so, how?
  • how can I create a my resource in a way that I don't have to replicate the code for every resource? basically extending the current $resource object to override the configuration, or the parsing function.

1 Answer 1

2

What are you receiving back is an object and not an array, so use $resource.get, then you can access the results array from the object

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

6 Comments

But with the get I should get a single club. And I've the resource url set as /clubs/:Id . And actually I would like to have two different calls instead of ha in a single get functions for two porpouses.
If you can change what the API is returning, then you should change it to be just an array and not an array wrapped in an object. If you can't change what the API is returning to you, then you'll have to either add a new method to handle querying or override $resource.query. It's more work that way.
But isn't there anyway to extend the $resource class creating a myResource class that does not expect an array? how can I extend a angualr module/class? anyway it works with get() it just i don't like much the idea to use get to get all the result and also the datails.
To extend $resource, you would just create another service that wraps $resource and adds whatever additional methods you want. At that point it's really just creating extra work for yourself though as you'll likely end up just calling $resource.get under the hood of that service to access the collections you're returning. Angular is a very opinionated framework and the way the collection is wrapped goes against what Angular wants you to do.
I see, i'll go for your solution. In your last line " Angular is a very opinionated framework and the way the collection is wrapped goes against what Angular wants you to do." you mean in the case I will wrap the collection as I had in mind or generally speaking?
|

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.