0

I have an ember-data app that uses the ember-pouch adapter for local & remote storage.

I am unable to load hasMany relationships when the belongsTo side is polymorphic. I've played with async: true/false and donstsave: true/false options on the hasMany side, to no avail.

The setup:

  • post can have many comments.
  • comment can have many comments.
  • comment belongs to commentable.
// app/models/post.js
import DS from 'ember-data';
import { Model } from 'ember-pouch';

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

// app/models/comment.js
import DS from 'ember-data';
import { Model } from 'ember-pouch';

export default Model.extend({
    DS.belongsTo('commentable', { polymorphic: true });
    DS.hasMany('comment', { inverse: 'commentable' });
});

The Problem

Calling post.get('comments') loads nothing. If comments are loaded into the store separately, however, then post is able to correctly render comments:

// In the console (being mindful that `post.get('comments')` returns a promise)

const post = store.findRecord('post', '123');
post.get('comments').get('length'); // => 0

store.findAll('comment');
post.get('comments').get('length'); // => 12
Community
  • 1
  • 1
pmlord
  • 66
  • 4

1 Answers1

0

What worked for me during an experiment (although I was heavily modifying ember-pouch within the adapter) was using post.get('comments').content.length but don't ask me why this is so and if it is supposed to be that way ...

EDIT: It seems the problem is that the data is not loaded at that time. So probably something like post.get('comments').then(function() {this.debug(post.get('comments').length})) will work.

ultravail
  • 31
  • 3