Ember获取ember数据中的相关记录1.13.8 [英] Ember get related record in ember-data 1.13.8

查看:73
本文介绍了Ember获取ember数据中的相关记录1.13.8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

预扫描数据1.13.8我能够使用以下语法获取相关的记录(这是在符合embers从视图移动的组件内):

  var user = this.get('user')//将用户模型传递给组件
var classifications = user.get(Classifications); //这将是相关记录

但是在ember 1.13.8 分类现在是 undefined ,而之前是一系列相关记录。我遵循了转换指南,但是似乎没有找到参考这个变化。



我的用户模型看起来像这样:

 导出默认DS.Model.extend({
用户:DS.attr('string'),
电子邮件:DS.attr('string'),
PCTID:DS.attr('number'),
分类:DS.hasMany('classification'),
});

和我的分类模型如下所示:

 导出默认DS.Model.extend({
分类:DS.attr('string'),
发布时间:DS.attr('string'),
DatePosted:DS.attr('isodate'),
公告:DS.attr('string'),
ExpireDate:DS .attr('isodate'),
标题:DS.attr('string'),
用户:DS.belongsTo('user'),
UserClassification:DS.attr()
});

用户和它的 classifiedations 正在返回商店并序列化为以下格式:

  {
data:{
type:user,
id:1361,
attributes:{
User:foo ,
电子邮件:null,
PCTID:1
}
},
included:[
{
类型:分类,
id:1,
属性:{
分类:房间,
已发布的 ,
DatePosted:2014-09-17T00:00:00.000Z,
公告:所有数据都是虚构的,
ExpireDate:null,
Title:bar for foo,
User:1361,
UserClassification:{
UserId:1361,
ClassificationId:1
}
}
}
]
}

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

解决方案

好吧,我想出来了。它似乎与ember数据1.13.8和移动到JSON-API您需要显式包括在JSON响应中的关系,不能让Ember通过定义关系来照顾他们在你的模型。 (个人来说,这将增加一个看似不必要的开发开销,因为我正在依靠和填补对我的关系,而不是我必须弄明白什么是我的API中的一个相关文档,然后将其序列化为JSON-API格式)



我成功格式化的JSON看起来像这样:

  {
data:{
type:user,
id:1361 ,
属性:{
用户:foo,
电子邮件:null,
PCTID:1
},
关系:{
分类:{
data:[
{id:1,type:classification,}
]
}
}
},
included:[
{
type:classification,
id:1 ,
属性:{
分类:房间,
已发布:P Hauser
DatePosted:2014-09-17T00:00:00.000Z,
公告:所有数据都是虚构的,
ExpireDate:null,
标题:bar for foo,
User:1361,
UserClassification:{
UserId:1361,
ClassificationId:1
}
}
}
]
}


Pre ember-data 1.13.8 I was able to get related records with the following syntax (this is inside of a component to comply with embers move from views):

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

However in ember 1.13.8 classifications is now undefined whereas before it was an array of related records. I have followed the transition guide but can't seem to find a reference to this change.

My user model looks like this:

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

and my classification model looks like this:

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(),
});

Both the user and it's classifiations are being returned to the store and serialized into the following format:

{
   "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
            }
         }
      }
   ]
}

How should I be getting the related records in ember-data 1.13.8?

解决方案

Ok, so I figured it out. It looks like with ember-data 1.13.8 and the move to JSON-API you need to explicitly include relationships in your JSON response and can't just let ember look after them for you by defining the relationship in your model.

(Personally this will add a seemingly unnecessary development overhead, as I was relying and ember sorting the relationships out for me, instead of me having to figure out what is a related document from my API and then serialize them into a JSON-API format)

My successfully formatted JSON looked like this:

{
   "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
            }
         }
      }
   ]
}

这篇关于Ember获取ember数据中的相关记录1.13.8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆