Skip to content

Commit 24db727

Browse files
committed
fix(object): revert 1ee8e6 partially to fix undetected changes when
saving
1 parent f705b55 commit 24db727

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

src/object.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ const {
99
findValue,
1010
} = require('./utils');
1111

12+
const recursiveToPointer = value => {
13+
if (_.isObject(value)) {
14+
if (value._toPointer) return value._toPointer();
15+
return _.mapObject(value, recursiveToPointer);
16+
}
17+
if (_.isArray(value)) return value.map(recursiveToPointer);
18+
return value;
19+
};
20+
1221
const RESERVED_KEYS = ['objectId', 'createdAt', 'updatedAt'];
1322
const checkReservedKey = key => {
1423
if (RESERVED_KEYS.indexOf(key) !== -1) {
@@ -311,10 +320,6 @@ module.exports = function(AV) {
311320
dirty: function(attr) {
312321
this._refreshCache();
313322

314-
return this._dirty();
315-
},
316-
317-
_dirty: function(attr) {
318323
var currentChanges = _.last(this._opSetQueue);
319324

320325
if (attr) {
@@ -567,8 +572,7 @@ module.exports = function(AV) {
567572
!(value instanceof AV.Object) &&
568573
!(value instanceof AV.File)
569574
) {
570-
value = value.toJSON ? value.toJSON() : value;
571-
var json = JSON.stringify(value);
575+
var json = JSON.stringify(recursiveToPointer(value));
572576
if (this._hashedJSON[key] !== json) {
573577
var wasSet = !!this._hashedJSON[key];
574578
this._hashedJSON[key] = json;
@@ -593,16 +597,15 @@ module.exports = function(AV) {
593597
AV._arrayEach(this._opSetQueue, function(opSet) {
594598
var op = opSet[key];
595599
if (op) {
596-
const [value, actualTarget, actualKey] = findValue(
600+
const [value, actualTarget, actualKey, firstKey] = findValue(
597601
self.attributes,
598602
key
599603
);
600604
setValue(self.attributes, key, op._estimate(value, self, key));
601605
if (actualTarget && actualTarget[actualKey] === AV.Op._UNSET) {
602606
delete actualTarget[actualKey];
603-
} else {
604-
self._resetCacheForKey(key);
605607
}
608+
self._resetCacheForKey(firstKey);
606609
}
607610
});
608611
},
@@ -1604,7 +1607,7 @@ module.exports = function(AV) {
16041607
AV.Object._findUnsavedChildren = function(objects, children, files) {
16051608
AV._traverse(objects, function(object) {
16061609
if (object instanceof AV.Object) {
1607-
if (object._dirty()) {
1610+
if (object.dirty()) {
16081611
children.push(object);
16091612
}
16101613
return;

src/utils/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ const setValue = (target, key, value) => {
208208

209209
const findValue = (target, key) => {
210210
const segs = key.split('.');
211+
const firstSeg = segs[0];
211212
const lastSeg = segs.pop();
212213
let currentTarget = target;
213214
for (let i = 0; i < segs.length; i++) {
@@ -217,7 +218,7 @@ const findValue = (target, key) => {
217218
}
218219
}
219220
const value = currentTarget[lastSeg];
220-
return [value, currentTarget, lastSeg];
221+
return [value, currentTarget, lastSeg, firstSeg];
221222
};
222223

223224
module.exports = {

test/object.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -756,17 +756,31 @@ describe('Objects', function() {
756756
className: 'C',
757757
objectId: id,
758758
},
759+
d: {
760+
__type: 'Pointer',
761+
className: 'C',
762+
objectId: id,
763+
objects: [{}],
764+
},
759765
});
760766
const b = a.get('b')[0];
761767
const c = a.get('c');
768+
const d = a.get('d');
762769
b.set({ a });
763-
a._dirty().should.eql(false);
764-
b._dirty().should.eql(true);
765-
c._dirty().should.eql(false);
770+
a.dirty().should.eql(false);
771+
b.dirty().should.eql(true);
772+
c.dirty().should.eql(false);
773+
d.dirty().should.eql(false);
774+
d.get('objects')[0].foo = 'bar';
775+
a.dirty().should.eql(false);
776+
b.dirty().should.eql(true);
777+
c.dirty().should.eql(false);
778+
d.dirty().should.eql(true);
766779
c.set({ a });
767-
a._dirty().should.eql(false);
768-
b._dirty().should.eql(true);
769-
c._dirty().should.eql(true);
780+
a.dirty().should.eql(false);
781+
b.dirty().should.eql(true);
782+
c.dirty().should.eql(true);
783+
d.dirty().should.eql(true);
770784
});
771785
});
772786
}); //END RETRIEVING

0 commit comments

Comments
 (0)