Skip to content
This repository has been archived by the owner on Aug 20, 2024. It is now read-only.

reset _manyToOneDeleted after save #18

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions addon/mixins/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ const resetRelations = function(record) {
});
});
record.set('__recordsJustSaved', []);

let savedBelongsTo = record.get('__savedBelongsToRecords');
savedBelongsTo.forEach((record) => {
record.set('_manyToOneDeleted', null);
});
return record;
};

Expand Down
11 changes: 8 additions & 3 deletions addon/mixins/nested-relations.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { merge } from 'lodash/object';
// and one not.
// This is only required for hasMany's
let savedRecords = {};
let savedBelongsToRecords = {};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed here? If I understand correctly, we can get this value above and try to call forEach on it?


const iterateRelations = function(record, relations, callback) {
Object.keys(relations).forEach((relationName) => {
Expand Down Expand Up @@ -138,11 +139,13 @@ const hasManyData = function(parent, relationName, relatedRecords, subRelations,
return { data: payloads };
};

const belongsToData = function(relatedRecord, subRelations, isManyToOneDelete, includedRecords) {
const belongsToData = function(parent, relatedRecord, subRelations, isManyToOneDelete, includedRecords) {
let payload = jsonapiPayload(relatedRecord, isManyToOneDelete);
processRelationships(subRelations, payload, relatedRecord, includedRecords);
addToIncludes(payload, includedRecords);

if (isManyToOneDelete) {
savedBelongsToRecords.push(parent);
}
return { data: payloadForRelationship(payload) };
};

Expand All @@ -152,7 +155,7 @@ const processRelationship = function(parent, name, kind, relationData, subRelati
if (kind === 'hasMany') {
payload = hasManyData(parent, name, relationData, subRelations, manyToManyDeleted, includedRecords);
} else {
payload = belongsToData(relationData, subRelations, isManyToOneDelete, includedRecords);
payload = belongsToData(parent, relationData, subRelations, isManyToOneDelete, includedRecords);
}

if (payload && payload.data) {
Expand Down Expand Up @@ -199,6 +202,7 @@ const relationshipsDirective = function(value) {
export default Mixin.create({
serialize(snapshot/*, options */) {
savedRecords = [];
savedBelongsToRecords = [];

let json = this._super(...arguments);
let includedRecords = [];
Expand Down Expand Up @@ -237,6 +241,7 @@ export default Mixin.create({
json.included = includedRecords;
}
snapshot.record.set('__recordsJustSaved', savedRecords);
snapshot.record.set('__savedBelongsToRecords', savedBelongsToRecords);
}

return json;
Expand Down