2014-10-06 74 views
0

我想配置一个角度$资源。这个$资源应该允许调用远程Web服务。Angular js嵌套资源

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

在我的方案中,我想要使用第一个Web服务检索所有嵌套的资源。当用户修改嵌套资源时,将使用第三个Web服务保存嵌套资源。

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

这种配置很适合打到第二和第三个Web服务,但是如何配置资源来使用第一个。几个函数在嵌套的资源原型上注册,我想避免创建特定的$资源。

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

回答

1

创建的资源自定义操作如下:

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

现在,你应该能够做到:

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

检索列表parentId的1

+0

谢谢。我如何指定父资源ID? – gontard 2014-10-06 07:57:02

+0

看到我的更新 - 我不知道我现在明白你的问题,但希望这有助于。 – Fordio 2014-10-06 08:36:19

+0

正是我在找的东西。谢谢 – gontard 2014-10-06 08:47:39