【问题标题】:Ember Model RESTAdapter nested endpointEmber 模型 RESTAdapter 嵌套端点
【发布时间】:2014-05-16 15:11:06
【问题描述】:

我正在使用 Ember 1.5 构建一个 Web 应用程序,并使用 Ember 模型 0.0.11 将该应用程序链接到 API。我目前无法让 Ember 模型使用嵌套 API 端点。

例如,我的应用有一个User 模型,它与Post 模型有hasMany 关系。现在,当我想为某个用户加载帖子时,我希望Post.findQuery('user_id', {user_id}); 方法可以访问GET /users/{user_id}/posts 端点。相反,Ember 模型解决方案似乎更喜欢将查询作为参数发送到 GET /posts 端点。

Ember Model 的 RESTAdapter 确实允许轻松自定义,但在我开始编写完全自定义的 RESTAdapter 之前,我很想知道是否有人已经这样做了,以及他们是如何解决问题的。

【问题讨论】:

    标签: rest ember.js nested endpoint ember-model


    【解决方案1】:

    我们处理嵌套 API 路由的解决方案是使用请求的有效负载返回链接哈希。如果您可以更改 API 输出,那么这是目前的方法。例如,在请求工作空间时,返回的 JSON 如下所示(请原谅转义字符):

    {
       "data":
          {
             "object":"workspace",
             "id":"wrk_krVZWGaJ",
             "organization_name":"Legros, Klein and Boehm",
             "workspace_name":"Legros, Klein and Boehm",
             "workspace_path":"legros, klein and boehm",
             "status":true,
             "credit_production":7,
             "credit_revision":16,
             "links":{
                "projects":"\/v1\/workspaces\/wrk_krVZWGaJ\/projects",
                "productions":"\/v1\/workspaces\/wrk_krVZWGaJ\/productions",
                "subscription":"\/v1\/workspaces\/wrk_krVZWGaJ\/subscription",
                "assets":"\/v1\/workspaces\/wrk_krVZWGaJ\/assets",
                "descriptions":"\/v1\/workspaces\/wrk_krVZWGaJ\/descriptions",
                "roles":"\/v1\/workspaces\/wrk_krVZWGaJ\/roles",
                "registrations":"\/v1\/workspaces\/wrk_krVZWGaJ\/registrations",
                "users":"\/v1\/workspaces\/wrk_krVZWGaJ\/users"
             }
          }
    }
    

    模型将链接定义为 async hasMany 关系,即DS.attr('projects', {async: true})。从商店加载工作区时,不包括链接的项目。当您在其他任何地方使用{{#each workspace.projects}}workspace.get('projects') 时,ember-data 将向/v1/workspaces/wrk_krVZWGaJ/projects/ 发出GET 请求,而不是/projects/ 的默认路由。尼托,嗯?

    更多详情请关注RESTAdapter findHasMany method

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-26
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多