【问题标题】:Angular and Rails shallow resourcesAngular 和 Rails 浅层资源
【发布时间】:2013-03-28 10:14:23
【问题描述】:

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 之外的另一个模块?还是我应该只构建两个服务:评论和评论?

【问题讨论】:

  • 顺便说一句,同样的问题对资源上的成员和集合操作有效,需要更改资源 URL。

标签: ruby-on-rails rest angularjs


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2011-10-16
    • 1970-01-01
    • 2015-11-30
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多