Rhinon
Rhinon

Reputation: 377

ember-data: How to tell if a model's async: true relationship is loaded without triggering a load?

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

Upvotes: 10

Views: 2587

Answers (6)

Jacob van Lingen
Jacob van Lingen

Reputation: 9537

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;

Upvotes: 7

Michael Edgar
Michael Edgar

Reputation: 420

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')) {...}

Upvotes: 5

Trey Griffith
Trey Griffith

Reputation: 509

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')

Upvotes: 0

H1D
H1D

Reputation: 758

update. Still not in public API =(

For now I use this:

s._data.yourRelationshipName.get('isLoaded')

Upvotes: 1

Amr Draz
Amr Draz

Reputation: 2796

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

Upvotes: 0

Kingpin2k
Kingpin2k

Reputation: 47367

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) {...}

Upvotes: 3

Related Questions