Testing, if belongs to a relationship, has been populated

I have an ember data model that is relevant belongsTo

and I would like to check if there is any value (aka foreign key reference) in this relationship. At first I thought I could just specify:

if(myModel.rel !== null) {
    // do something now that belongsTo relationship has a value
}

      

But of course this doesn't work because it myModel.rel

will never be null and will instead be a kind of Ember Data object. Good. I adjusted this:

if(myModel.rel.content !== null) {
    // do something now that belongsTo relationship has a value
}

      

It works, but I feel like maybe this is a little too hacky ... is there a cleaner, more API that specifies this conditional in Ember Data?

+3


source to share


1 answer


I know this is an old question, but I did it this way (I don't know about official

).

if (model.get('relationshipName.id')) {
  // there and ID present, so it means theres a value for the foreign key
}

      



model.relationshipName.id

returns undefined

when there is no value and id when there is value.

0


source







All Articles