【问题标题】:Is it possible to have nested objects in Angular resource?Angular资源中是否可以嵌套对象?
【发布时间】:2015-11-10 11:49:15
【问题描述】:

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

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

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

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

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

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

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

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

【问题讨论】:

  • 我通常做的是 -> 使“星”也是一个可选参数:星。然后我只是将它作为参数传递给对象。效果很好。
  • @wtflux - 是的,谢谢,我认为 Petr 的回答正是您所说的,不是吗?

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


【解决方案1】:

你可以使用额外的参数:

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

【讨论】:

    猜你喜欢
    • 2014-02-20
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多