Ember在ember-data 1.13.8中获得相关记录

时间:2015-08-11 14:13:51

标签: javascript ember.js ember-data

Pre ember-data 1.13.8我能够使用以下语法ffmpeg.exe -i xxx.bin out.yuv 相关记录(这是一个符合余烬从视图移动的组件内部):

get

但是在ember 1.13.8中,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 现在是classifications,而在它之前是一系列相关记录。我已经关注transition guide,但似乎无法找到对此更改的引用。

我的undefined模型如下所示:

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(), }); 及其user都将返回商店并按照以下格式序列化:

classifiations

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

1 个答案:

答案 0 :(得分:0)

好的,所以我明白了。看起来像使用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
            }
         }
      }
   ]
}