1
votes

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?

1
Is your user model loaded when you call user.get("Classifications") or is it still in flight?GJK
Classifications: DS.hasMany('classification', { async: true }), you should also use lower case variables (unrelated just convention), what you want to do is always set relationships to async: truePatsy Issa
@GJK The user model is always loaded with the classifications model, I've added the serialized version of the response.Phil Hauser
@Kitler, The UpperCamelCase is due to the fact that I am working on a legacy system where the convention was to UpperCamelCase case (and I chickened out and kept it that way like a coward!! :p lol) . Also as far as I was aware { async: true } is now the default and has thus been deprecated.Phil Hauser
I don't think I'm formatting my JSON properly into a JSON-API formatted response, I haven't separated relationships outPhil Hauser

1 Answers

0
votes

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