4

Rails 为我依赖的嵌套资源提供了一个 :shallow 选项,否则我将拥有 4 或 5 个嵌套资源,这将非常糟糕。

浅层资源如下所示:

resources :posts do
  resources :comments, :only => [:index, :create]
end
resources :comments, :only => [:show, :update, :destroy]

我遇到的问题是在尝试使用 Angular 资源来映射 Rails 资源时,因为 ngResource 只允许定义单个 URI,所以要么/posts/:post_id/comments/:id要么/comments/:id.

我想要的是 ngResource 允许方法覆盖 URI 和插值。例如:

app.factory('Comment', ['ngResource', function ($resource) {
    $resource('/comments/:id', { 'id': '@id', post_id: '@post_id' },
        index:   { method: 'GET',  url: '/posts/:post_id/comments', isArray: true },
        create:  { method: 'POST', url: '/posts/:post_id/comments' },
        show:    { method: 'GET' },
        update:  { method: 'PUT' },
        destroy: { method: 'DELETE' }
    );
}]);

这可能吗,也许是 ngResource 之外的另一个模块?或者我应该只构建两个服务:评论和评论?

4

1 回答 1

2

我实际上是从 github 上查看了 angular 资源的源代码,发现当前 master 允许操作指定自己的 url 参数。我在我的问题中写的示例完全可以使用 Angular 1.1.4+(尚未发布)。

升级到当前的角度大师是解决方案。

于 2013-03-28T10:51:25.113 回答