Skip to content

Commit ee6259b

Browse files
author
Lee Richmond
committed
Avoid unloading records marked for deletion
1 parent 8f5e28c commit ee6259b

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

addon/mixins/model.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import Ember from 'ember';
22

33
const resetRelations = function(record) {
4-
record.get('__recordsJustSaved').forEach((r) => {
5-
let shouldUnload = r.get('isNew') || r.get('markedForDestruction') || r.get('markedForDeletion');
6-
if (shouldUnload) {
7-
r.unloadRecord();
8-
}
4+
Object.keys(record.get('__recordsJustSaved')).forEach((relationName) => {
5+
let relationRecords = record.get('__recordsJustSaved')[relationName];
6+
7+
relationRecords.forEach((r) => {
8+
let shouldUnload = r.get('isNew') || r.get('markedForDestruction');
9+
if (shouldUnload) {
10+
r.unloadRecord();
11+
} else if (r.get('markedForDeletion')) {
12+
record.get(relationName).removeObject(r);
13+
r.set('markedForDeletion', false);
14+
}
15+
});
916
});
1017
record.set('__recordsJustSaved', []);
1118
};

addon/mixins/nested-relations.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Ember from 'ember';
55
// we will be left with 2 of the same object - one persisted
66
// and one not.
77
// This is only required for hasMany's
8-
let savedRecords = [];
8+
let savedRecords = {};
99

1010
const iterateRelations = function(record, relations, callback) {
1111
Object.keys(relations).forEach((relationName) => {
@@ -72,13 +72,14 @@ const jsonapiPayload = function(record) {
7272
return payload;
7373
};
7474

75-
const hasManyData = function(relatedRecords, subRelations) {
75+
const hasManyData = function(relationName, relatedRecords, subRelations) {
7676
let payloads = [];
77+
savedRecords[relationName] = [];
7778
relatedRecords.forEach((relatedRecord) => {
7879
let payload = jsonapiPayload(relatedRecord);
7980
processRelationships(subRelations, payload, relatedRecord);
8081
payloads.push(payload);
81-
savedRecords.push(relatedRecord);
82+
savedRecords[relationName].push(relatedRecord);
8283
});
8384
return { data: payloads };
8485
};
@@ -89,11 +90,11 @@ const belongsToData = function(relatedRecord, subRelations) {
8990
return { data: payload };
9091
};
9192

92-
const processRelationship = function(kind, relationData, subRelations, callback) {
93+
const processRelationship = function(name, kind, relationData, subRelations, callback) {
9394
let payload = null;
9495

9596
if (kind === 'hasMany') {
96-
payload = hasManyData(relationData, subRelations);
97+
payload = hasManyData(name, relationData, subRelations);
9798
} else {
9899
payload = belongsToData(relationData, subRelations);
99100
}
@@ -108,7 +109,7 @@ const processRelationships = function(relationshipHash, jsonData, record) {
108109
jsonData.relationships = {};
109110

110111
iterateRelations(record, relationshipHash, (name, kind, related, subRelations) => {
111-
processRelationship(kind, related, subRelations, (payload) => {
112+
processRelationship(name, kind, related, subRelations, (payload) => {
112113
jsonData.relationships[name] = payload;
113114
});
114115
});

0 commit comments

Comments
 (0)