Skip to content

Commit 1591f92

Browse files
author
leon dmello
committed
Allow marking many to many relation for deletion
1 parent 18496c4 commit 1591f92

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

addon/mixins/model.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default Ember.Mixin.create({
4040
markForDeletion() {
4141
this.set('_markedForDeletion', true);
4242
},
43-
43+
4444
unmarkForDeletion() {
4545
this.set('_markedForDeletion', false);
4646
},
@@ -53,6 +53,34 @@ export default Ember.Mixin.create({
5353
this.set('_markedForDestruction', false);
5454
},
5555

56+
markManyToManyDeletion(relation, model) {
57+
let deletedRelations = this.get('_manyToManyDeleted');
58+
if(!deletedRelations) {
59+
this.set('_manyToManyDeleted', Ember.Object.create());
60+
deletedRelations = this.get('_manyToManyDeleted');
61+
}
62+
63+
if(!deletedRelations.get(relation)) {
64+
deletedRelations.set(relation, Ember.A());
65+
}
66+
67+
if(!deletedRelations.get(relation).includes(model)) {
68+
deletedRelations.get(relation).pushObject(model);
69+
}
70+
},
71+
72+
manyToManyMarkedForDeletion(relation) {
73+
return this.get('_manyToManyDeleted') &&
74+
this.get(`_manyToManyDeleted.${relation}`);
75+
},
76+
77+
unmarkManyToManyDeletion(relation, model) {
78+
return this.get('_manyToManyDeleted') &&
79+
this.get(`_manyToManyDeleted.${relation}`) &&
80+
this.get(`_manyToManyDeleted.${relation}`).removeObject(model);
81+
},
82+
83+
5684
jsonapiType() {
5785
return this.store
5886
.adapterFor(this.constructor.modelName)

addon/mixins/nested-relations.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ const iterateRelations = function(record, relations, callback) {
1111
Object.keys(relations).forEach((relationName) => {
1212
let subRelations = relations[relationName];
1313

14-
let metadata = record.relationshipFor(relationName);
15-
let kind = metadata.kind;
16-
let relatedRecord = record.get(relationName);
14+
let metadata = record.relationshipFor(relationName);
15+
let kind = metadata.kind;
16+
let relatedRecord = record.get(relationName);
17+
let manyToManyDeleted = record.manyToManyMarkedForDeletion(relationName);
18+
1719

1820
if (metadata.options.async !== false) {
1921
relatedRecord = relatedRecord.get('content');
2022
}
2123

2224
if (relatedRecord) {
23-
callback(relationName, kind, relatedRecord, subRelations);
25+
callback(relationName, kind, relatedRecord, subRelations, manyToManyDeleted);
2426
}
2527
});
2628
};
@@ -29,7 +31,7 @@ const isPresentObject = function(val) {
2931
return val && Object.keys(val).length > 0;
3032
};
3133

32-
const attributesFor = function(record) {
34+
const attributesFor = function(record, isManyToManyDelete) {
3335
let attrs = {};
3436

3537
let changes = record.changedAttributes();
@@ -43,7 +45,7 @@ const attributesFor = function(record) {
4345
}
4446
});
4547

46-
if (record.get('markedForDeletion')) {
48+
if (record.get('markedForDeletion') || isManyToManyDelete) {
4749
attrs = { _delete: true };
4850
}
4951

@@ -54,8 +56,8 @@ const attributesFor = function(record) {
5456
return attrs;
5557
};
5658

57-
const jsonapiPayload = function(record) {
58-
let attributes = attributesFor(record);
59+
const jsonapiPayload = function(record, isManyToManyDelete) {
60+
let attributes = attributesFor(record, isManyToManyDelete);
5961

6062
let payload = { type: record.jsonapiType() };
6163

@@ -70,11 +72,11 @@ const jsonapiPayload = function(record) {
7072
return payload;
7173
};
7274

73-
const hasManyData = function(relationName, relatedRecords, subRelations) {
75+
const hasManyData = function(relationName, relatedRecords, subRelations, manyToManyDeleted) {
7476
let payloads = [];
7577
savedRecords[relationName] = [];
7678
relatedRecords.forEach((relatedRecord) => {
77-
let payload = jsonapiPayload(relatedRecord);
79+
let payload = jsonapiPayload(relatedRecord, manyToManyDeleted && manyToManyDeleted.includes(relatedRecord));
7880
processRelationships(subRelations, payload, relatedRecord);
7981
payloads.push(payload);
8082
savedRecords[relationName].push(relatedRecord);
@@ -88,11 +90,11 @@ const belongsToData = function(relatedRecord, subRelations) {
8890
return { data: payload };
8991
};
9092

91-
const processRelationship = function(name, kind, relationData, subRelations, callback) {
93+
const processRelationship = function(name, kind, relationData, subRelations, manyToManyDeleted, callback) {
9294
let payload = null;
9395

9496
if (kind === 'hasMany') {
95-
payload = hasManyData(name, relationData, subRelations);
97+
payload = hasManyData(name, relationData, subRelations, manyToManyDeleted);
9698
} else {
9799
payload = belongsToData(relationData, subRelations);
98100
}
@@ -106,8 +108,8 @@ const processRelationships = function(relationshipHash, jsonData, record) {
106108
if (isPresentObject(relationshipHash)) {
107109
jsonData.relationships = {};
108110

109-
iterateRelations(record, relationshipHash, (name, kind, related, subRelations) => {
110-
processRelationship(name, kind, related, subRelations, (payload) => {
111+
iterateRelations(record, relationshipHash, (name, kind, related, subRelations, manyToManyDeleted) => {
112+
processRelationship(name, kind, related, subRelations, manyToManyDeleted, (payload) => {
111113
let serializer = record.store.serializerFor(record.constructor.modelName);
112114
let serializedName = serializer.keyForRelationship(name);
113115
jsonData.relationships[serializedName] = payload;

0 commit comments

Comments
 (0)