How to get all currently loaded related records of a hasMany relationship without resolving it

I have a large one-to-many relationship modeled by ember-data hasMany.

// models/parent.js
export default DS.Model.extend({
  children: DS.hasMany('child'), // may have hundreds of children
// models/child.js
export default DS.Model.extend({
  parent: DS.belongsTo('parent'),

Since a parent has so many children, I do not want to resolve the relationship all at once.

// Avoid this, since it resolves all the children at once
const childrenPromise = parent.get('children');

Instead, I’m implementing pagination. As far as I’ve seen, there is currently no built-in pagination for ember-data. So I’m implementing a custom solution, using the opaque query() adapter method and passing in the relevant paging information.

Here’s my question: For a given parent record, I need access to an array of all children currently loaded in the client-side store – without resolving the relationship. Furthermore, I need the array to update automatically as records in the store are changed, added, or removed.

Is there an easy way to do this? If not, what will it take to make it happen?

Could I use the ds-references functionality to achieve this without forcing the relationship to resolve? If so, is there a clean way to put the necessary code in the parent model, so I can simply query a property of the parent record and be given a live-updating array of related records?

parent.hasMany('children').value() // <- synchronous, will not fetch

1 Like

Thank you for your quick reply!

The documentation you linked to says the following:

If the relationship is not loaded it will always return null.

What does it mean for the relationship to not be “loaded” in this context? I thought the whole idea of the hasMany().value() method is to get the relationship without loading it.

If it means the method returns null when there are no records in the relationship, this could pose a problem. If no records have been loaded into the relationship, I need my model’s method to return an empty array that will be populated asynchronously as records are added into the store.