EmbeddedRecordsMixin: Embedded hasMany is only visible after explicitly calling store.find()


#1

I do have a comment model, which has an 1-n relationship to reply. Replies are always retrieved embedded in the same response - therefore I am using the EmbeddedRecordsMixin:

model/comment.js:

export default DS.Model.extend({
  replies: DS.hasMany('reply'),
  post: DS.belongsTo('post', { inverse: 'replies'}),
  
  replyCount: Ember.computed('replies.[]', function() {
    return this.get('reply.length');
  })
})

model/reply.js:

export default DS.Model.extend({
  comment: DS.belongsTo('comment', { inverse: 'replies'}),
});

serializer/comment.js:

export default Serializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    replies: {
      embedded: 'always'
    }
  }
})

this all has been working really well, until I started to make use of computed properties within the comment model. If the comment is directly loaded from the store via this.get('store').findRecord('comment', id), the computed property for the reply count is correct (no matter if findRecord triggers an actual request to the server or not). However, if the comment is accessed via a reference on the parent model (a post, mapped via belongsTo and hasMany) the replies are an array of length 0. If I have a look in the ember inspector, the replies are shown as <computed>. If I click on that, it resolves into <(unknown mixin):ember...>, retriggers the computed property and has the reply array correctly initialised with all data (no request to the server is being made). If I call findRecord on the comment after it has been loaded and shows the empty replies, the returned record is correctly initialised (without a request to the server).

model/post.js:

export default DS.Model.extend({
  replies: DS.hasMany('reply')
})

Has anyone ever seen anything similar and does have an idea what to check? I have been through everything twice and can not find any reasonable explanation why this is happening.


Fetch nested relationship data fails
#2

So, after some more debugging, I think I found the cause: The hasMany relationship does not return the records directly, but an EachProxy which seems to works like a promise. Therefore I need to wait for the promise on the post to be resolved post.get('comments').then(() => { ... }) before doing anything with the comments.