I need to check if an async relationship has been loaded without triggering the load, is this possible?
6 Answers
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;

- 8,989
- 7
- 48
- 78
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')) {...}

- 350
- 3
- 12
-
1This 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
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) {...}

- 47,277
- 10
- 78
- 96
update. Still not in public API =(
For now I use this:
s._data.yourRelationshipName.get('isLoaded')

- 748
- 1
- 6
- 18
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

- 2,806
- 3
- 20
- 26
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')

- 499
- 4
- 8