Skip to content

Commit e354afa

Browse files
author
robin
committed
Revert to previous implementation
1 parent 2167009 commit e354afa

3 files changed

Lines changed: 45 additions & 47 deletions

File tree

dist/mobx-spine.cjs.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,23 +1246,22 @@ var Model = (_class$1 = (_temp$1 = _class2$1 = function () {
12461246
// Set the changes for all related models with changes
12471247
source.__activeCurrentRelations.forEach(function (relation) {
12481248
if (relation && source[relation]) {
1249-
if (!_this6[relation]) {
1250-
// Sometimes a nested model has relations that were not defined in the starting object,
1251-
// these need to be copied as well
1252-
_this6[relation] = new source[relation].constructor({ relations: source[relation].__activeRelations });
1253-
_this6[relation].parse(source[relation].toJS());
1254-
}
1255-
if (source[relation].hasUserChanges) {
1256-
if (source[relation].models) {
1257-
// If related item is a store
1258-
// Set the changes for all related models with changes
1259-
source[relation].models.forEach(function (relatedModel, index) {
1260-
_this6[relation].models[index].__copyChanges(relatedModel, _this6[relation]);
1261-
});
1262-
} else {
1263-
// Set the changes for the related model
1264-
_this6[relation].__copyChanges(source[relation], undefined);
1249+
if (_this6[relation]) {
1250+
if (source[relation].hasUserChanges) {
1251+
if (source[relation].models) {
1252+
// If related item is a store
1253+
// Set the changes for all related models with changes
1254+
source[relation].models.forEach(function (relatedModel, index) {
1255+
_this6[relation].models[index].__copyChanges(relatedModel, _this6[relation]);
1256+
});
1257+
} else {
1258+
// Set the changes for the related model
1259+
_this6[relation].__copyChanges(source[relation], undefined);
1260+
}
12651261
}
1262+
} else {
1263+
// Related object not in relations of the model we are copying
1264+
console.warn('Found related object ' + source.constructor.backendResourceName + ' with relation ' + relation + ',\n which is not defined in the relations of the model you are copying. Skipping ' + relation + '.');
12661265
}
12671266
}
12681267
});

dist/mobx-spine.es.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,23 +1240,22 @@ var Model = (_class$1 = (_temp$1 = _class2$1 = function () {
12401240
// Set the changes for all related models with changes
12411241
source.__activeCurrentRelations.forEach(function (relation) {
12421242
if (relation && source[relation]) {
1243-
if (!_this6[relation]) {
1244-
// Sometimes a nested model has relations that were not defined in the starting object,
1245-
// these need to be copied as well
1246-
_this6[relation] = new source[relation].constructor({ relations: source[relation].__activeRelations });
1247-
_this6[relation].parse(source[relation].toJS());
1248-
}
1249-
if (source[relation].hasUserChanges) {
1250-
if (source[relation].models) {
1251-
// If related item is a store
1252-
// Set the changes for all related models with changes
1253-
source[relation].models.forEach(function (relatedModel, index) {
1254-
_this6[relation].models[index].__copyChanges(relatedModel, _this6[relation]);
1255-
});
1256-
} else {
1257-
// Set the changes for the related model
1258-
_this6[relation].__copyChanges(source[relation], undefined);
1243+
if (_this6[relation]) {
1244+
if (source[relation].hasUserChanges) {
1245+
if (source[relation].models) {
1246+
// If related item is a store
1247+
// Set the changes for all related models with changes
1248+
source[relation].models.forEach(function (relatedModel, index) {
1249+
_this6[relation].models[index].__copyChanges(relatedModel, _this6[relation]);
1250+
});
1251+
} else {
1252+
// Set the changes for the related model
1253+
_this6[relation].__copyChanges(source[relation], undefined);
1254+
}
12591255
}
1256+
} else {
1257+
// Related object not in relations of the model we are copying
1258+
console.warn('Found related object ' + source.constructor.backendResourceName + ' with relation ' + relation + ',\n which is not defined in the relations of the model you are copying. Skipping ' + relation + '.');
12601259
}
12611260
}
12621261
});

src/Model.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -487,22 +487,22 @@ export default class Model {
487487
// Set the changes for all related models with changes
488488
source.__activeCurrentRelations.forEach((relation) => {
489489
if (relation && source[relation]) {
490-
if (!this[relation]) {
491-
// Sometimes a nested model has relations that were not defined in the starting object,
492-
// these need to be copied as well
493-
this[relation] = new source[relation].constructor({ relations: source[relation].__activeRelations });
494-
this[relation].parse(source[relation].toJS());
495-
}
496-
if (source[relation].hasUserChanges) {
497-
if (source[relation].models) { // If related item is a store
498-
// Set the changes for all related models with changes
499-
source[relation].models.forEach((relatedModel, index) => {
500-
this[relation].models[index].__copyChanges(relatedModel, this[relation]);
501-
});
502-
} else {
503-
// Set the changes for the related model
504-
this[relation].__copyChanges(source[relation], undefined)
490+
if (this[relation]) {
491+
if (source[relation].hasUserChanges) {
492+
if (source[relation].models) { // If related item is a store
493+
// Set the changes for all related models with changes
494+
source[relation].models.forEach((relatedModel, index) => {
495+
this[relation].models[index].__copyChanges(relatedModel, this[relation]);
496+
});
497+
} else {
498+
// Set the changes for the related model
499+
this[relation].__copyChanges(source[relation], undefined)
500+
}
505501
}
502+
} else {
503+
// Related object not in relations of the model we are copying
504+
console.warn(`Found related object ${source.constructor.backendResourceName} with relation ${relation},
505+
which is not defined in the relations of the model you are copying. Skipping ${relation}.`)
506506
}
507507
}
508508
});

0 commit comments

Comments
 (0)