Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
_findBelongsToByJsonApiResource(resource, parentInternalModel, relationshipMeta, options) {
if (!resource) {
return RSVP.resolve(null);
}
// TODO not this
let hasData = resource.data !== null;
let internalModel = hasData ? this._internalModelForResource(resource.data) : null;
let {
isStale: relationshipIsStale,
hasReceivedData: hasAnyRelationshipData,
isEmpty: relationshipIsEmpty,
} = resource.getState();
// TODO probably a better thing to check here than this
let hasDematerializedInverse = hasData ? internalModel.isEmpty() : false;
let allInverseRecordsAreLoaded = hasData ? !internalModel.isEmpty() : true;
hasAnyRelationshipData = hasAnyRelationshipData || parentInternalModel.isNew();
let shouldFindViaLink =
_findHasManyByJsonApiResource(resource, parentInternalModel, relationshipMeta, options) {
if (!resource) {
return RSVP.resolve([]);
}
let hasResourceData = Array.isArray(resource.data) && resource.data.length > 0;
let internalModels = hasResourceData
? resource.data.map(i => this._internalModelForResource(i))
: [];
let {
isStale: relationshipIsStale,
hasReceivedData: hasAnyRelationshipData,
isEmpty: relationshipIsEmpty,
} = resource.getState();
// TODO probably a better thing to check here than this
let hasDematerializedInverse = hasResourceData
? internalModels.reduce((c, i) => c || i.isEmpty(), false)
: false;
let allInverseRecordsAreLoaded = hasResourceData
return this.findMany(internalModels, options);
}
let hasData = hasAnyRelationshipData && !relationshipIsEmpty;
// fetch by data
if (hasData || hasLocalPartialData) {
let internalModels = resource.data.map(json => this._internalModelForResource(json));
return this._scheduleFetchMany(internalModels, options);
}
// we were explicitly told we have no data and no links.
// TODO if the relationshipIsStale, should we hit the adapter anyway?
return RSVP.resolve([]);
},
_fetchBelongsToLinkFromResource(resource, parentInternalModel, relationshipMeta, options) {
if (!resource || !resource.links || !resource.links.related) {
// should we warn here, not sure cause its an internal method
return RSVP.resolve(null);
}
return this.findBelongsTo(
parentInternalModel,
resource.links.related,
relationshipMeta,
options
).then(internalModel => {
let response = internalModel && recordDataFor(internalModel).getResourceIdentifier();
parentInternalModel.linkWasLoadedForRelationship(relationshipMeta.key, { data: response });
if (internalModel === null) {
return null;
}
// TODO Igor this doesn't seem like the right boundary, probably the caller method should extract the record out
return internalModel.getRecord();
});
},
// fetch using data, pulling from local cache if possible
if (!relationshipIsStale && (preferLocalCache || hasLocalPartialData)) {
/*
We have canonical data, but our local state is empty
*/
if (localDataIsEmpty) {
return RSVP.resolve(null);
}
return this._findByInternalModel(internalModel, options);
}
let resourceIsLocal = !localDataIsEmpty && resource.data.id === null;
if (resourceIsLocal) {
return RSVP.resolve(internalModel.getRecord());
}
// fetch by data
if (!localDataIsEmpty) {
return this._scheduleFetch(internalModel, options).then(() => {
return internalModel.getRecord();
});
}
// we were explicitly told we have no data and no links.
// TODO if the relationshipIsStale, should we hit the adapter anyway?
return RSVP.resolve(null);
},
// fetch using data, pulling from local cache if possible
if (!relationshipIsStale && (preferLocalCache || hasLocalPartialData)) {
/*
We have canonical data, but our local state is empty
*/
if (localDataIsEmpty) {
return RSVP.resolve(null);
}
return this._findByInternalModel(internalModel, options);
}
let resourceIsLocal = !localDataIsEmpty && resource.data.id === null;
if (resourceIsLocal) {
return RSVP.resolve(internalModel.getRecord());
}
// fetch by data
if (!localDataIsEmpty) {
return this._scheduleFetch(internalModel, options).then(() => {
return internalModel.getRecord();
});
}
// we were explicitly told we have no data and no links.
// TODO if the relationshipIsStale, should we hit the adapter anyway?
return RSVP.resolve(null);
},