What options exist to allow nested resources in ngResource responses?
There were some related questions about resolving endpoints for a nested resource in ngResource, but this question is related to when the REST answer contains a second resource nested in the requested collection, especially 1 to 1 mappings, where you would like pets/<id>/owner as your own resource.
Let's say that there are two resources: Pets and Owners :
GET / Pets:
[{ name: 'spark', type: 'dog', owner: '/owners/3/'
As a developer, I sometimes want to request the Owner resource as a whole, sometimes I want to request the Pet resource, and then I automatically want to enable the Owner attribute in the resource instance.
This is my current solution:
.factory('Pet', function ($resource, Owner) { var Pet = $resource('/pets/:id', {id: '@id'}); Pet.prototype.getOwner = function () { return new Owner(this.owner);
There are many problems. There is one integrity. This implementation, I believe, allows you to use multiple instances of the same resource. Then there is practicality. You also have additional attributes for tracking ( Owner and getOwner() , not just Owner , possibly setOwner if you want to save the model).
An alternative solution could be built on transformResponse , but it would be like hacks to include this in every resource that has a nested mapping.
angularjs angularjs-resource
lyschoening
source share