10

I need to check if an async relationship has been loaded without triggering the load, is this possible?

Rhinon
  • 337
  • 1
  • 3
  • 8

6 Answers6

7

After time has passed, Ember Data 2.5 got released. One of implemented features is the ds-references feature.

The references API allows to interact with your relationships. With it, it is possible to check if your RelationshipName is already loaded, without triggering a request:

model.hasMany('yourRelationshipName').value() !== null;
Jacob van Lingen
  • 8,989
  • 7
  • 48
  • 78
5

With ember-data 1.13, the following will work for a hasMany relationship. Still a hack, but there does not seem to be a public API.

var relationships = model._internalModel._relationships.initializedRelationships;

if (relationships.yourRelationshipName.manyArray.get('isLoaded')) {...}
Michael Edgar
  • 350
  • 3
  • 12
  • 1
    This is the only method that works for me, using Ember-Data 1.13.5. Note, for some reason, `relationships.myRelationshipName` sometimes was undefined for me until the array was loaded, so I had to do `if (relationships.myRelationshipName && relationships.myRelationshipName.manyArray.get('isLoaded'))` – stephen.hanson Aug 01 '15 at 17:00
  • Also, better yet, I've been playing up with this in Ember Data 2.12.1, and I ended up settling for this variant instead: `Ember.get(relationships 'relationshipName.hasLoaded')`. I found that the thing with the `_manyArray` told me the relationship was not loaded in cases where it was actually loaded from the server, but it had not been accessed so far via `model.get('relationshipName')`. – Ernesto Mar 29 '17 at 14:42
3

There is no official way to accomplish this yet, but unofficially you can do

// this will be null if not loaded, and populated if it has at least started loading
if(model._relationships.yourRelationshipName) {...}
Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
1

update. Still not in public API =(

For now I use this:

s._data.yourRelationshipName.get('isLoaded')
H1D
  • 748
  • 1
  • 6
  • 18
0

I'm adding this cause model._relationships.yourRelationshipName didn't work for me

model._data.yourRelationshipName did work tho will also be null if not loaded, and populated if it has at least started loading

Amr Draz
  • 2,806
  • 3
  • 20
  • 26
0

For a hasMany relationship the non-public API for accessing this in Ember Data 1.0.0-beta.12 is model._relationships.tasks.manyArray.get('isLoaded')

Trey Griffith
  • 499
  • 4
  • 8