【问题标题】:Ember get related record in ember-data 1.13.8Ember 在 ember-data 1.13.8 中获取相关记录
【发布时间】:2015-11-03 19:46:06
【问题描述】:

在 ember-data 1.13.8 之前,我能够使用以下语法 get 相关记录(这是在组件内部以符合 embers 从视图中移动):

var user = this.get('user') // The user model is passed through to the component
var classifications = user.get("Classifications"); // This would be the related records

但是在 ember 1.13.8 中,classifications 现在是 undefined,而之前它是一组相关记录。我已关注transition guide,但似乎找不到有关此更改的参考。

我的user 模型如下所示:

export default DS.Model.extend({
    User: DS.attr('string'),
    Email: DS.attr('string'),
    PCTID: DS.attr('number'),
    Classifications: DS.hasMany('classification'),
});

我的classification 模型看起来像这样:

export default DS.Model.extend({
    Classification: DS.attr('string'),
    PostedBy: DS.attr('string'),
    DatePosted: DS.attr('isodate'),
    Bulletin: DS.attr('string'),
    ExpireDate: DS.attr('isodate'),
    Title: DS.attr('string'),
    User: DS.belongsTo('user'),
    UserClassification: DS.attr(),
});

userclassifiations 都将返回到商店并序列化为以下格式:

{
   "data": {
      "type": "user",
      "id": 1361,
      "attributes": {
         "User": "foo",
         "Email": null,
         "PCTID": 1
      }
   },
   "included": [
      {
         "type": "classification",
         "id": 1,
         "attributes": {
            "Classification": "Room",
            "PostedBy": "P Hauser",
            "DatePosted": "2014-09-17T00:00:00.000Z",
            "Bulletin": "All data is fictitious",
            "ExpireDate": null,
            "Title": "Bar for foo",
            "User": 1361,
            "UserClassification": {
               "UserId": 1361,
               "ClassificationId": 1
            }
         }
      }
   ]
}

我应该如何获取 ember-data 1.13.8 中的相关记录?

【问题讨论】:

  • 当你调用user.get("Classifications")时你的用户模型是否已经加载或者它还在运行中?
  • Classifications: DS.hasMany('classification', { async: true }),你也应该使用小写变量(无关只是约定),你要做的总是将关系设置为async: true
  • @GJK 用户模型总是加载分类模型,我添加了响应的序列化版本。
  • @Kitler,UpperCamelCase 是因为我正在开发一个传统系统,其中约定是 UpperCamelCase 案例(我退缩并像懦夫一样保持这种方式!!:p哈哈) 。据我所知,{ async: true } 现在是默认设置,因此已被弃用。
  • 我认为我没有将我的 JSON 正确格式化为 JSON-API 格式的响应,我没有将 relationships 分开

标签: javascript ember.js ember-data


【解决方案1】:

好的,所以我想通了。它看起来像使用 ember-data 1.13.8 和迁移到 JSON-API 您需要明确在您的 JSON 响应中包含关系并且不能只让 ember 通过定义关系来为您照顾它们在你的模型中。

(就个人而言,这会增加看似不必要的开发开销,因为我依赖并为我整理关系,而不是我必须从我的 API 中找出相关文档是什么,然后将它们序列化为JSON-API 格式)

我成功格式化的 JSON 如下所示:

{
   "data": {
      "type": "user",
      "id": 1361,
      "attributes": {
         "User": "foo",
         "Email": null,
         "PCTID": 1
      },
      "relationships": {
         "Classifications": {
             "data": [
                 {"id": 1, "type": "classification",}
             ]
         }
      }
   },
   "included": [
      {
         "type": "classification",
         "id": 1,
         "attributes": {
            "Classification": "Room",
            "PostedBy": "P Hauser",
            "DatePosted": "2014-09-17T00:00:00.000Z",
            "Bulletin": "All data is fictitious",
            "ExpireDate": null,
            "Title": "Bar for foo",
            "User": 1361,
            "UserClassification": {
               "UserId": 1361,
               "ClassificationId": 1
            }
         }
      }
   ]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    相关资源
    最近更新 更多