Feeding Ember Data hasMany belongsTo seems weird to me


#1

Pasted from the docs, two models

App.Post = DS.Model.extend({
  comments: DS.hasMany('comment', {async: true})
});

App.Comment = DS.Model.extend({
  post: DS.belongsTo('post')
});

Expected data format

{
  "post": {
    "comments": [1, 2, 3]
  }
}

{
  "comment": {
    "post": 1
  }
}

Having

"comments": [1, 2, 3]

Makes the whole thing awkward with a relational schema. Why not just

{
  "post": { }
}

{
  "comment": {
    "post": 1
  }
}

And starting with a post have Ember Data somehow make a request for all comments belonging to a post? Is it because there is no REST representation for belongsTo? Can an adapter be wired in somewhere to do this automatically so I don’t have to do weird json manipulations?


#2

In principle you can map the ids anyway you want inside an adapter.

I think the reason things are configured the way they are was to enable more reliable mapping between related models depending on which side you loaded first.

You might want to read more about the “Single Source of Truth” refactor of Ember Data that happened about a year ago.