Format of RESTSerializer


#1

What is a good place to find examples or documentation on what the RESTSerializer expects in Ember 2.6x?

I am struggling with the case where I have a model User with

export default Model.extend({
  username: attr(),
  pos: hasMany('pos')
});

My endpoint returns:

{
  "pos": [],
  "user": {
    "id": 3,
    "pos": [],
    "username": "sadfadsf"
  }
}

and that is getting retrieved from my controller with

return Ember.RSVP.hash({this.store.findRecord('user', user)});

However, if I try to pull the empty list of pos from the Ember debugger on that model I get a different object.

$E.user.get('pos')
Class {__ember1469049158432: "ember825", __ember_meta__: Meta}

I suspect that this is a promise that isn’t resolved properly?

However, I can grab the username just fine with E.user.get('username').

Any I passing in an incorrect format from the API? What aren’t the pos getting resolved?


#2

Setting async: true on the relationships seems to provide something more promising but doesn’t seems like the objects are using the Pos class:

$E.user.get('pos')
Class {canonicalState: Array[1], store: Class, relationship: ManyRelationship, record: InternalModel, currentState: Array[1]…}__ember1469051503745: "ember709"__ember_meta__: Meta_super: ROOT()canonicalState: Array[1]currentState: Array[1]isLoaded: trueisPolymorphic: undefinedlength: 1record: InternalModelrelationship: ManyRelationshipstore: Class__ember1469051503745: "ember598"__ember_meta__: Meta_backburner: Backburner_instanceCache: ContainerInstanceCache_pendingFetch: Map_pendingSave: Array[0]_super: ROOT()adapter: (...)get adapter: IGETTER_FUNCTION()set adapter: SETTER_FUNCTION(value)container: (...)recordArrayManager: ClasstypeMaps: Object__proto__: Classtype: tes@model:pos:__proto__: Class

How do I access the elements in this array? And is the expected format?


#3

Do some digging into http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html as that sounds like what you need :slight_smile:


#4

Thanks Ricky, was reading through the class but isn’t that only for embedded records for example nested records like

{
    "id": "1",
    "title": "Rails is omakase",
    "tag": "rails",
    "authors": [
        {
            "id": "2",
            "name": "Steve"
        }
    ]
}

To my understanding this may be an issue of my pos field (hasMany) not getting serialized.

Or maybe I am am misunderstanding haha.


#5

Your response needs to look like this:

{
  "user": {
    "id": 3,
    "pos": [1, 2, 3],
    "username": "sadfadsf"
  }
}

where pos will contain an array of ids. By default, when you access pos if the relationship is asynchronous, a request will be made for each pos record. Alternatively, you can sideload that data like this:

{
  "pos": [{ id: 1, amount: '...' }, { id: 2, amount: '...' }, { id: 3, amount: '...' }],
  "user": {
    "id": 3,
    "pos": [1, 2, 3],
    "username": "sadfadsf"
  }
}

I wrote about the RESTSerializer format in much more depth on my blog back in December that you might find useful: http://thejsguy.com/2015/12/05/which-ember-data-serializer-should-i-use.html