gontard
gontard

Reputation: 29550

Angular js nested resource

I want to configure an angular $resource. This $resource should allows to call distant web services.

// Gets all the nested-resources of a parent-resource
1) GET  /parent-resource/nested-resources
// Gets a nested-resource by id
2) GET  /nested-resource/{id}
// Create a nested resource
3) POST /nested-resource

In my scenario, i want retrieve all the nested resources using the first web service. When a nested resource is modified by the user, the nested resource will be save using the third web service.

var NestedResource = $resource('/nested-resource/:id');
Publishable.prototype.isNew = function() {
    return this.id === undefined;
};
... lot of functions...

This configuration works well to hit the 2nd and 3rd web services but how can i configure the resource to use the 1st. Several functions are register on the nested resource prototype, i would like to avoid the creation of a specific $resource.

Publishable.prototype.getByParentId = function(parentId) {
    // ??        
}

Upvotes: 0

Views: 116

Answers (1)

Fordio
Fordio

Reputation: 3830

Create a custom action on the resource as follows:

var NestedResource = $resource('/nested-resource/:id',,
    { action: "getAll",
      method: "GET",
      isArray: true,
      url: '/parent-resource/:parentId/nested-resources'
    });

Now you should be able to do:

var allItems = NestedResource.getAll({parentId: 1});

to retrieve the list for parentId 1.

Upvotes: 1

Related Questions