Created
May 17, 2019 07:19
-
-
Save patrixr/a61db552ff3eea2f0e6c1caa05c7635c to your computer and use it in GitHub Desktop.
Ember: Finding loaded associations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hasMissingAssociations(record, associationName) { | |
if (_.isArray(associationName)) { | |
return _.some(associationName, name => { | |
hasMissingAssociations(record, name); | |
}); | |
} | |
const relationship = record.relationshipFor(associationName); | |
if (!relationship) { | |
return false; | |
} | |
const associationModelName = relationship.type.replace(/-/g, '_'); | |
const idKey = _.camelCase(associationModelName) + (relationship.kind === "hasMany" ? 'Ids' : 'Id'); | |
const ids = _.flatten(record.get(idKey)); | |
return _.some(ids, (id) => { | |
return !store.peekRecord(associationModelName, id); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment