Remove records from the store when they are no longer in array responses
Ember Data doesn't do this by default, instead opting to be as non-destructive as possible. However, this is desired behavior.
This commit is contained in:
parent
5f10940c92
commit
34872543f9
|
@ -1,3 +1,5 @@
|
|||
import { copy } from '@ember/object/internals';
|
||||
import { get } from '@ember/object';
|
||||
import { makeArray } from '@ember/array';
|
||||
import JSONSerializer from 'ember-data/serializers/json';
|
||||
|
||||
|
@ -35,8 +37,29 @@ export default JSONSerializer.extend({
|
|||
documentHash.included.push(...included);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
store.push(documentHash);
|
||||
store.push(documentHash);
|
||||
},
|
||||
|
||||
normalizeArrayResponse(store, modelClass) {
|
||||
const result = this._super(...arguments);
|
||||
this.cullStore(store, modelClass.modelName, result.data);
|
||||
return result;
|
||||
},
|
||||
|
||||
// When records are removed server-side, and therefore don't show up in requests,
|
||||
// the local copies of those records need to be unloaded from the store.
|
||||
cullStore(store, type, records) {
|
||||
const newRecords = copy(records).filter(record => get(record, 'id'));
|
||||
const oldRecords = store.peekAll(type);
|
||||
oldRecords.filter(record => get(record, 'id')).forEach(old => {
|
||||
const newRecord = newRecords.find(record => get(record, 'id') === get(old, 'id'));
|
||||
if (!newRecord) {
|
||||
store.unloadRecord(old);
|
||||
} else {
|
||||
newRecords.removeObject(newRecord);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
|
|
Loading…
Reference in a new issue