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.
This commit is contained in:
parent
72cba3b036
commit
2ed5b61358
|
@ -2,6 +2,7 @@ import { inject as service } from '@ember/service';
|
|||
import { computed, get } from '@ember/object';
|
||||
import RESTAdapter from 'ember-data/adapters/rest';
|
||||
import codesForError from '../utils/codes-for-error';
|
||||
import removeRecord from '../utils/remove-record';
|
||||
|
||||
export const namespace = 'v1';
|
||||
|
||||
|
@ -45,27 +46,7 @@ export default RESTAdapter.extend({
|
|||
.peekAll(relationshipType)
|
||||
.filter(record => record.get(`${inverse.name}.id`) === snapshot.id)
|
||||
.forEach(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: relationshipType,
|
||||
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);
|
||||
removeRecord(store, record);
|
||||
});
|
||||
}
|
||||
return payload;
|
||||
|
|
|
@ -2,6 +2,7 @@ import { copy } from '@ember/object/internals';
|
|||
import { get } from '@ember/object';
|
||||
import { makeArray } from '@ember/array';
|
||||
import JSONSerializer from 'ember-data/serializers/json';
|
||||
import removeRecord from '../utils/remove-record';
|
||||
|
||||
export default JSONSerializer.extend({
|
||||
primaryKey: 'ID',
|
||||
|
@ -59,7 +60,7 @@ export default JSONSerializer.extend({
|
|||
.forEach(old => {
|
||||
const newRecord = newRecords.find(record => get(record, 'id') === get(old, 'id'));
|
||||
if (!newRecord) {
|
||||
store.unloadRecord(old);
|
||||
removeRecord(store, old);
|
||||
} else {
|
||||
newRecords.removeObject(newRecord);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
// 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);
|
||||
}
|
Loading…
Reference in New Issue