Search code examples
angularjsapirestrestful-urlangular-resource

Is it possible to have nested objects in Angular resource?


I'm building an RESTful API. I have a Person object in my model and every Person object has a IsStarred boolean property. So, for the Person objects, I've created a "people" resource that looks like this:

// Performs CRUD operations on this resource.
api/people/:id 

In order to preserve the RESTful model, I have the following resource that changes the IsStarred property of a given Person object:

// POST method sets IsStarred property to true, while DELETE method sets it to false.
// Example: POST api/people/2/star => Sets IsStarred = true to Person with Id == 2
// DELETE api/people/2/star => Sets IsStarred = false to Person with Id == 2
api/people/:id/star 

Now, I want to use the same strucutre in an Angular app, using $resource. I already have a Person resource that looks like this:

var Person= $resource('api/people/:id', { id: '@id' });

And I can use this Person resource in the well-known way.

By the way, now if I decide to edit the resource and make it look like this:

var Person= $resource('api/people/:id/star', { id: '@id' });

I can now use only the (un)star functionality, meaning I would have to make two different resources. Is this my only option or there's another one?


Solution

  • You can use additional param:

    var Person = $resource('api/people/:id:mode', { id: '@id'}, 
        get : {method : 'GET', params : {mode : ''}}, 
        post : {method : 'POST', params : {mode : '/star'}});