Skip to content

Commit 9d80547

Browse files
author
robin
committed
Cherry-pick changes of default negative ids on updated master
Ref T32139
1 parent ed1abb3 commit 9d80547

6 files changed

Lines changed: 375 additions & 135 deletions

File tree

dist/mobx-spine.cjs.js

Lines changed: 85 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -857,14 +857,40 @@ var Model = (_class$1 = (_temp$1 = _class2$1 = function () {
857857
value: function getNegativeId() {
858858
return -parseInt(this.cid.replace('m', ''));
859859
}
860+
861+
/**
862+
* Get InternalId returns the id of a model or a negative id if the id is not set
863+
* @returns {*} the id of a model or a negative id if the id is not set
864+
*/
865+
860866
}, {
861867
key: 'getInternalId',
862868
value: function getInternalId() {
863-
if (this.isNew) {
869+
if (!this[this.constructor.primaryKey]) {
864870
return this.getNegativeId();
865871
}
866872
return this[this.constructor.primaryKey];
867873
}
874+
875+
/**
876+
* Gives the model the internal id, meaning that it will keep the set id of the model or will receive a negative
877+
* id if the id is null. This is useful if you have a new model that you want to give an id so that it can be
878+
* referred to in a relation.
879+
*/
880+
881+
}, {
882+
key: 'assignInternalId',
883+
value: function assignInternalId() {
884+
this[this.constructor.primaryKey] = this.getInternalId();
885+
}
886+
887+
/**
888+
* The get url returns the url for a model., it appends the id if there is one. If the model is new it should not
889+
* append an id.
890+
*
891+
* @returns {string} the url for a model
892+
*/
893+
868894
}, {
869895
key: 'casts',
870896
value: function casts() {
@@ -895,12 +921,18 @@ var Model = (_class$1 = (_temp$1 = _class2$1 = function () {
895921
key: 'url',
896922
get: function get$$1() {
897923
var id = this[this.constructor.primaryKey];
898-
return '' + lodash.result(this, 'urlRoot') + (id ? id + '/' : '');
924+
return '' + lodash.result(this, 'urlRoot') + (!this.isNew ? id + '/' : '');
899925
}
926+
927+
/**
928+
* A model is considered new if it does not have an id, or if the id is a negative integer.
929+
* @returns {boolean} True if the model id is not set or a negative integer
930+
*/
931+
900932
}, {
901933
key: 'isNew',
902934
get: function get$$1() {
903-
return !this[this.constructor.primaryKey];
935+
return !this[this.constructor.primaryKey] || this[this.constructor.primaryKey] < 0;
904936
}
905937
}, {
906938
key: 'isLoading',
@@ -966,6 +998,16 @@ var Model = (_class$1 = (_temp$1 = _class2$1 = function () {
966998
if (options.relations) {
967999
this.__parseRelations(options.relations);
9681000
}
1001+
1002+
// The model will automatically be assigned a negative id, the id will still be overridden if it is supplied in the data
1003+
this.assignInternalId();
1004+
1005+
// We want our id to remain negative on a clear, only if it was not created with the id set to null
1006+
// which is usually the case when the object is a related model in which case we want the id to be reset to null
1007+
if (data && data[this.constructor.primaryKey] !== null || !data) {
1008+
this.__originalAttributes[this.constructor.primaryKey] = this[this.constructor.primaryKey];
1009+
}
1010+
9691011
if (data) {
9701012
this.parse(data);
9711013
}
@@ -1014,7 +1056,8 @@ var Model = (_class$1 = (_temp$1 = _class2$1 = function () {
10141056
if (RelModel.prototype instanceof Store) {
10151057
return new RelModel(options);
10161058
}
1017-
return new RelModel(null, options);
1059+
// If we have a related model, we want to force the related model to have id null as that means there is no model set
1060+
return new RelModel(defineProperty({}, RelModel.primaryKey, null), options);
10181061
}));
10191062
}
10201063

@@ -1043,15 +1086,15 @@ var Model = (_class$1 = (_temp$1 = _class2$1 = function () {
10431086
value: function toBackend() {
10441087
var _this4 = this;
10451088

1046-
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
1089+
var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
10471090

1048-
var _ref$data = _ref.data,
1049-
data = _ref$data === undefined ? {} : _ref$data,
1050-
_ref$mapData = _ref.mapData,
1051-
mapData = _ref$mapData === undefined ? function (x) {
1091+
var _ref2$data = _ref2.data,
1092+
data = _ref2$data === undefined ? {} : _ref2$data,
1093+
_ref2$mapData = _ref2.mapData,
1094+
mapData = _ref2$mapData === undefined ? function (x) {
10521095
return x;
1053-
} : _ref$mapData,
1054-
options = objectWithoutProperties(_ref, ['data', 'mapData']);
1096+
} : _ref2$mapData,
1097+
options = objectWithoutProperties(_ref2, ['data', 'mapData']);
10551098

10561099
var output = {};
10571100
// By default we'll include all fields (attributes+relations), but sometimes you might want to specify the fields to be included.
@@ -1169,8 +1212,8 @@ var Model = (_class$1 = (_temp$1 = _class2$1 = function () {
11691212
* Cloning the changes requires recursion over all related models that have changes or are related to a model with changes.
11701213
* Cloning
11711214
*
1172-
* @param source {Model} - The model that should be copied
1173-
* @param options {{}} - Options, {copyChanges - only copy the changed attributes, requires recursion over all related objects with changes}
1215+
* @param source {Model} The model that should be copied
1216+
* @param options {{}} Options, {copyChanges - only copy the changed attributes, requires recursion over all related objects with changes}
11741217
*/
11751218

11761219
}, {
@@ -1216,8 +1259,8 @@ var Model = (_class$1 = (_temp$1 = _class2$1 = function () {
12161259
/**
12171260
* Goes over model and all related models to set the changed values and notify the store
12181261
*
1219-
* @param source - the model to copy
1220-
* @param store - the store of the current model, to setChanged if there are changes
1262+
* @param source the model to copy
1263+
* @param store the store of the current model, to setChanged if there are changes
12211264
* @private
12221265
*/
12231266

@@ -1250,12 +1293,15 @@ var Model = (_class$1 = (_temp$1 = _class2$1 = function () {
12501293
if (source[relation].hasUserChanges) {
12511294
if (source[relation].models) {
12521295
// If related item is a store
1253-
// Check if the store has some changes
1254-
_this6[relation].__setChanged = source[relation].__setChanged;
1255-
// Set the changes for all related models with changes
1256-
source[relation].models.forEach(function (relatedModel, index) {
1257-
_this6[relation].models[index].__copyChanges(relatedModel, _this6[relation]);
1258-
});
1296+
if (source[relation].models.length === _this6[relation].models.length) {
1297+
// run only if the store shares the same amount of items
1298+
// Check if the store has some changes
1299+
_this6[relation].__setChanged = source[relation].__setChanged;
1300+
// Set the changes for all related models with changes
1301+
source[relation].models.forEach(function (relatedModel, index) {
1302+
_this6[relation].models[index].__copyChanges(relatedModel, _this6[relation]);
1303+
});
1304+
}
12591305
} else {
12601306
// Set the changes for the related model
12611307
_this6[relation].__copyChanges(source[relation], undefined);
@@ -1334,14 +1380,14 @@ var Model = (_class$1 = (_temp$1 = _class2$1 = function () {
13341380

13351381
}, {
13361382
key: '__scopeBackendResponse',
1337-
value: function __scopeBackendResponse(_ref2) {
1383+
value: function __scopeBackendResponse(_ref3) {
13381384
var _this8 = this;
13391385

1340-
var data = _ref2.data,
1341-
targetRelName = _ref2.targetRelName,
1342-
repos = _ref2.repos,
1343-
mapping = _ref2.mapping,
1344-
reverseMapping = _ref2.reverseMapping;
1386+
var data = _ref3.data,
1387+
targetRelName = _ref3.targetRelName,
1388+
repos = _ref3.repos,
1389+
mapping = _ref3.mapping,
1390+
reverseMapping = _ref3.reverseMapping;
13451391

13461392
var scopedData = null;
13471393
var relevant = false;
@@ -1407,13 +1453,13 @@ var Model = (_class$1 = (_temp$1 = _class2$1 = function () {
14071453

14081454
}, {
14091455
key: 'fromBackend',
1410-
value: function fromBackend(_ref3) {
1456+
value: function fromBackend(_ref4) {
14111457
var _this9 = this;
14121458

1413-
var data = _ref3.data,
1414-
repos = _ref3.repos,
1415-
relMapping = _ref3.relMapping,
1416-
reverseRelMapping = _ref3.reverseRelMapping;
1459+
var data = _ref4.data,
1460+
repos = _ref4.repos,
1461+
relMapping = _ref4.relMapping,
1462+
reverseRelMapping = _ref4.reverseRelMapping;
14171463

14181464
// We handle the fromBackend recursively. On each relation of the source model
14191465
// fromBackend gets called as well, but with data scoped for itself
@@ -1872,7 +1918,13 @@ var Model = (_class$1 = (_temp$1 = _class2$1 = function () {
18721918
var _this19 = this;
18731919

18741920
lodash.forIn(this.__originalAttributes, function (value, key) {
1875-
_this19[key] = value;
1921+
// If it is our primary key, and the primary key is negative, we generate a new negative pk, else we set it
1922+
// to the value
1923+
if (key === _this19.constructor.primaryKey && value < 0) {
1924+
_this19[key] = -1 * lodash.uniqueId();
1925+
} else {
1926+
_this19[key] = value;
1927+
}
18761928
});
18771929

18781930
this.__activeCurrentRelations.forEach(function (currentRel) {

0 commit comments

Comments
 (0)