mercredi 31 mai 2017

Emberfire: How to access parent models from embedded child models?

According to the emberfire guide, it is advised to define embedded relationships the following way:

// app/models/post.js

import DS from 'ember-data';
export default DS.Model.extend({
  comments: DS.hasMany('comment', { async: false, inverse: null }),
});

That is, set inverse to nullin the options of the relationship.

This makes perfect sense, since this causes the references to the parent model to not be saved in the child model, and we don't need those because of the embedding.

However there is no word about how to define the children. Obviously one would need to define the relationship in the child model somehow (if one wants to access the parent), for example:

// app/models/comment.js

import DS from 'ember-data';
export default DS.Model.extend({
  post: DS.belongsTo('post')
});

But this doesn't work. The parent isn't accessible. I even tried to define the inverse in the child ( { inverse: 'comments' } )

Is there a way to do this, or is this maybe a bug / shortcoming of emberfire at the moment?




Aucun commentaire:

Enregistrer un commentaire