Skip to content

Commit 8e6a371

Browse files
author
John M Naglick
committed
add manyToOne deletion
1 parent 200a9d8 commit 8e6a371

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

addon/mixins/model.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ const defaultOptions = function(options) {
2929
};
3030

3131
export default Mixin.create({
32-
hasDirtyAttributes: computed('currentState.isDirty', 'markedForDestruction', 'markedForDeletion', function() {
32+
hasDirtyAttributes: computed('currentState.isDirty', 'markedForDestruction', 'markedForDeletion', '_manyToOneDeleted.[]', function() {
3333
let original = this._super(...arguments);
34-
return original || this.get('markedForDestruction') || this.get('markedForDeletion');
34+
return original || this.get('markedForDestruction') || this.get('markedForDeletion') || this.get('_manyToOneDeleted.length') > 0;
3535
}),
3636

3737
markedForDeletion: computed('_markedForDeletion', function() {
@@ -58,6 +58,27 @@ export default Mixin.create({
5858
this.set('_markedForDestruction', false);
5959
},
6060

61+
markManyToOneDeletion(relation) {
62+
let deletedRelations = this.get('_manyToOneDeleted');
63+
64+
if (!deletedRelations) {
65+
this.set('_manyToOneDeleted', A());
66+
deletedRelations = this.get('_manyToOneDeleted')
67+
}
68+
69+
if (!deletedRelations.includes(relation)) {
70+
deletedRelations.pushObject(relation)
71+
}
72+
},
73+
74+
unmarkManyToOneDeletion(relation) {
75+
return this.markedForManyToOneDeletion(relation) && this.get('_manyToOneDeleted').removeObject(relation)
76+
},
77+
78+
markedForManyToOneDeletion(relation) {
79+
return this.get('_manyToOneDeleted') && this.get('_manyToOneDeleted').includes(relation);
80+
},
81+
6182
markManyToManyDeletion(relation, model) {
6283
let deletedRelations = this.get('_manyToManyDeleted');
6384
if(!deletedRelations) {

addon/mixins/nested-relations.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ const iterateRelations = function(record, relations, callback) {
1717
let kind = metadata.kind;
1818
let relatedRecord = record.get(relationName);
1919
let manyToManyDeleted = record.manyToManyMarkedForDeletionModels(relationName);
20+
let isManyToOneDelete = record.markedForManyToOneDeletion(relationName);
2021

2122
if (metadata.options.async !== false) {
2223
relatedRecord = relatedRecord.get('content');
2324
}
2425

2526
if (relatedRecord) {
26-
callback(relationName, kind, relatedRecord, subRelations, manyToManyDeleted);
27+
callback(relationName, kind, relatedRecord, subRelations, manyToManyDeleted, isManyToOneDelete);
2728
}
2829
});
2930
};
@@ -53,7 +54,7 @@ const attributesFor = function(record) {
5354
return attrs;
5455
};
5556

56-
const jsonapiPayload = function(record, isManyToManyDelete) {
57+
const jsonapiPayload = function(record, relationshipMarkedForDeletion) {
5758
let attributes = attributesFor(record);
5859

5960
let payload = { type: record.jsonapiType() };
@@ -69,7 +70,7 @@ const jsonapiPayload = function(record, isManyToManyDelete) {
6970
else if (record.get('markedForDestruction')) {
7071
payload['method'] = 'destroy';
7172
}
72-
else if (record.get('markedForDeletion') || isManyToManyDelete) {
73+
else if (record.get('markedForDeletion') || relationshipMarkedForDeletion) {
7374
payload['method'] = 'disassociate';
7475
}
7576
else if (record.get('currentState.isDirty')) {
@@ -131,21 +132,21 @@ const hasManyData = function(relationName, relatedRecords, subRelations, manyToM
131132
return { data: payloads };
132133
};
133134

134-
const belongsToData = function(relatedRecord, subRelations, includedRecords) {
135-
let payload = jsonapiPayload(relatedRecord);
135+
const belongsToData = function(relatedRecord, subRelations, isManyToOneDelete, includedRecords) {
136+
let payload = jsonapiPayload(relatedRecord, isManyToOneDelete);
136137
processRelationships(subRelations, payload, relatedRecord, includedRecords);
137138
addToIncludes(payload, includedRecords);
138139

139140
return { data: payloadForRelationship(payload) };
140141
};
141142

142-
const processRelationship = function(name, kind, relationData, subRelations, manyToManyDeleted, includedRecords, callback) {
143+
const processRelationship = function(name, kind, relationData, subRelations, manyToManyDeleted, isManyToOneDelete, includedRecords, callback) {
143144
let payload = null;
144145

145146
if (kind === 'hasMany') {
146147
payload = hasManyData(name, relationData, subRelations, manyToManyDeleted, includedRecords);
147148
} else {
148-
payload = belongsToData(relationData, subRelations, includedRecords);
149+
payload = belongsToData(relationData, subRelations, isManyToOneDelete, includedRecords);
149150
}
150151

151152
if (payload && payload.data) {
@@ -157,8 +158,8 @@ const processRelationships = function(relationshipHash, jsonData, record, includ
157158
if (isPresentObject(relationshipHash)) {
158159
jsonData.relationships = {};
159160

160-
iterateRelations(record, relationshipHash, (name, kind, related, subRelations, manyToManyDeleted) => {
161-
processRelationship(name, kind, related, subRelations, manyToManyDeleted, includedRecords, (payload) => {
161+
iterateRelations(record, relationshipHash, (name, kind, related, subRelations, manyToManyDeleted, isManyToOneDelete) => {
162+
processRelationship(name, kind, related, subRelations, manyToManyDeleted, isManyToOneDelete, includedRecords, (payload) => {
162163
let serializer = record.store.serializerFor(record.constructor.modelName);
163164
let serializedName = serializer.keyForRelationship(name);
164165
jsonData.relationships[serializedName] = payload;

0 commit comments

Comments
 (0)