-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat(ngResource): support extending resources #7308
base: master
Are you sure you want to change the base?
Conversation
Thanks for the PR! Please check the items below to help us merge this faster. See the contributing docs for more information.
If you need to make changes to your pull request, you can update the commit with Thanks again for your help! |
I'm still not sold that saving them into the original resource would be a bad thing; wouldn't that mean the api has a model with a property that is also the name of another model? That situation doesn't seem too too likely. The automatic saving means you would be able to retain the use of your original object instead of using another -
instead of
I see what you're saying about it assuming a little too much though - it could definitely cause undesirable behavior for people who are expecting
As devils advocate too, I think APIs who have these "tacked-on" methods for their models might not be considered by some to be best practice. Instead of having:
I think some people might argue that the API should be designed without the user/:id/children function:
That argument would mean that our suggestions were encouraging the use of bad practice api organization. I favor the first example but I know some people would be against tacking on those additional functions. |
Hi @dadleyy, thanks, I digg the saveAs suggestion, definitely cleaner. I do agree that some people won't consider the api/users/:id/children as good practices but the resource does not have to reflect the actual api. the actual endpoint can be /api/children or even /api2/children for that matter and can still be defined as an action on the api/users resource. Using the url option on the action it can really be anything. Regardless, the bug that causes a crash when calling an array action on an instance of a resource must be resolved. If you allow users to defined such an action (isArray) it shouldn't crash. |
call ngResource's response interceptor with the resource's instance as the context to give the user the abillity to reference it from within the interceptor function and set the response data on it Closes angular#4565
Awesome stuff! |
@mary-poppins so how to progress from here? |
02dc2aa
to
fd2d6c0
Compare
cad9560
to
f294244
Compare
e8dc429
to
e83fab9
Compare
4dd5a20
to
998c61c
Compare
A suggested solution for #4565
Request Type: feature
How to reproduce:
Component(s): ngResource
Impact: small
Complexity: small
This issue is related to:
When calling a Resource instance's
GET
actions such as$get
and$query
but mostly custom ones, it is sometimes needed to be able to extend the resource with the response data e.g:Automatically setting the response on the resource with the property name is a bad idea since it can lead to unexpected overrides (the resource might have had a property named children) and generally it should be up to the user to decide whether to extend or not and how.
My approach involves leveraging the Resource's interceptor to achieve a both flexible and clean way to extend instances. By calling the interceptor's response function with the instance as it's context (this) it is possible to reference it and set the desired properties:
Update
As @dadleyy suggested,
I have added an optional param on actions called saveAs to give the user control over whether to save the response on the resource's instance and under what property name.