open-nomad/ui/app/utils/remove-record.js
Michael Lange 2ed5b61358 Repeat the relationship unlinking pattern in the serializer
It also culls for findAll requests, so it too needs to be
careful about leaving garbage around.
2018-04-06 18:07:57 -07:00

28 lines
875 B
JavaScript

// Unlinks a record from all its relationships and unloads it from
// the store.
export default function removeRecord(store, record) {
// Collect relationship property names and types
const relationshipMeta = [];
record.eachRelationship((key, { kind }) => {
relationshipMeta.push({ key, kind });
});
// Push an update to this record with the relationships nulled out.
// This unlinks the relationship from the models that aren't about to
// be unloaded.
store.push({
data: {
id: record.get('id'),
type: record.constructor.modelName,
relationships: relationshipMeta.reduce((hash, rel) => {
hash[rel.key] = { data: rel.kind === 'hasMany' ? [] : null };
return hash;
}, {}),
},
});
// Now that the record has no attachments, it can be safely unloaded
// from the store.
store.unloadRecord(record);
}