MCPcopy
hub / github.com/miragejs/miragejs / _disassociateFromHasManyInverses

Method _disassociateFromHasManyInverses

lib/orm/model.js:812–847  ·  view source on GitHub ↗
(association)

Source from the content-addressed store, hash-verified

810
811 // Disassociate currently persisted models that are no longer associated
812 _disassociateFromHasManyInverses(association) {
813 let fk = association.getForeignKey();
814 let tempAssociation =
815 this._tempAssociations && this._tempAssociations[association.name];
816 let associateIds = this.attrs[fk];
817
818 if (tempAssociation && associateIds) {
819 let models;
820 if (association.isPolymorphic) {
821 models = associateIds.map(({ type, id }) => {
822 return this._schema[this._schema.toCollectionName(type)].find(id);
823 });
824 } else {
825 // TODO: prob should initialize hasMany fks with []
826 models = this._schema[
827 this._schema.toCollectionName(association.modelName)
828 ].find(associateIds || []).models;
829 }
830
831 models
832 .filter(
833 (associate) =>
834 // filter out models that are already being saved
835 !associate.isSaving &&
836 // filter out models that will still be associated
837 !tempAssociation.includes(associate) &&
838 associate.hasInverseFor(association)
839 )
840 .forEach((associate) => {
841 let inverse = associate.inverseFor(association);
842
843 associate.disassociate(this, inverse);
844 associate.save();
845 });
846 }
847 }
848
849 /*
850 Disassociate currently persisted models that are no longer associated.

Callers 1

Calls 9

toCollectionNameMethod · 0.80
hasInverseForMethod · 0.80
inverseForMethod · 0.80
disassociateMethod · 0.80
getForeignKeyMethod · 0.45
findMethod · 0.45
filterMethod · 0.45
includesMethod · 0.45
saveMethod · 0.45

Tested by

no test coverage detected