如何在 JS-Data 资源上创建嵌套的自定义 class-action?

How can you create a nested, custom class-action on a JS-Data resource?

我有一个嵌套的 JS-Data 对象(即具有父关系)

(注意所有代码都是 coffeescript)

Follow = DS.defineResource
  name: "follow"
  endpoint: "follows"
  relations:
    belongsTo:
      post:
        localField: "post"
        localKey: "post_id"
        parent: true
  actions:
    currentUsersFollow:
      method: "GET"
      pathname: "current_users_follow"

我需要调用自定义操作,currentUsersFollow 但是我不想在 Follow 的实例上调用它。它是一个类似于 /index 的 class 方法,并且没有关联的实例。但是路径仍然需要嵌套在父对象中。

Follow.currentUsersFollow({post_id: 213423})
# => http://ourapp.com/api/v1/follows/current_users_follow 
#
# I would like this to generate:
# http://ourapp.com/api/v1/posts/213423/follows/current_users_follow 
#
# i.e. it's missing the nesting: /posts/213423

这可以通过 .findAll

现在,如果您使用 .findAll() 执行此操作,那么只要您传递相关的父 ID,它就会生成嵌套路由:

Follow.findAll({post_id: 213423})
# => http://ourapp.com/api/posts/213423/follows

如您所见,post 已正确指定并转到通用端点。

如何在没有实例的情况下创建自定义嵌套操作?

简而言之,我想为我的 JS-Data 资源创建一个自定义操作,该资源嵌套在父级中但未在子级实例上调用。

这可能吗?

定义您的操作:

actions: {
  currentUsersFollow: {
    method: 'GET',
    endpoint: 'posts',
    pathname: 'current_users_follow'
  }
}

然后这样称呼它:

Follow.currentUsersFollow(213423)

它应该会产生

GET /posts/213423/current_users_follow