是否可以在Angular资源中使用嵌套对象?

时间:2015-11-10 11:49:15

标签: angularjs api rest restful-url angular-resource

我正在构建一个RESTful API。我的模型中有一个Person对象,每个Person对象都有一个IsStarred布尔属性。因此,对于Person对象,我创建了一个“people”资源,如下所示:

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

为了保留RESTful模型,我有以下资源来更改给定Person对象的IsStarred属性:

// 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 

现在,我想使用$ resource在Angular应用程序中使用相同的结构。我已经拥有一个如下所示的Person资源:

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

我可以用众所周知的方式使用这个Person资源。

顺便说一下,现在如果我决定编辑资源并使它看起来像这样:

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

我现在只能使用(un)星形功能,这意味着我必须制作两种不同的资源。这是我唯一的选择还是另一个?

1 个答案:

答案 0 :(得分:1)

您可以使用其他参数:

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