Skip to content

Commit 14c37bd

Browse files
committed
fix(object): remove unset keys when fetching
1 parent 6e41f0d commit 14c37bd

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/object.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ module.exports = function(AV) {
162162
.then(function(response) {
163163
const results = _.map(objects, function(object, i) {
164164
if (response[i].success) {
165-
object._finishFetch(object.parse(response[i].success));
165+
const fetchedAttrs = object.parse(response[i].success);
166+
object._cleanupUnsetKeys(fetchedAttrs);
167+
object._finishFetch(fetchedAttrs);
166168
return object;
167169
}
168170
if (response[i].success === null) {
@@ -922,7 +924,7 @@ module.exports = function(AV) {
922924
* @return {Promise} A promise that is fulfilled when the fetch
923925
* completes.
924926
*/
925-
fetch: function(fetchOptions, options) {
927+
fetch: function(fetchOptions = {}, options) {
926928
var self = this;
927929
var request = _request(
928930
'classes',
@@ -933,11 +935,19 @@ module.exports = function(AV) {
933935
options
934936
);
935937
return request.then(function(response) {
936-
self._finishFetch(self.parse(response), true);
938+
const fetchedAttrs = self.parse(response);
939+
if (!fetchOptions.keys) self._cleanupUnsetKeys(fetchedAttrs);
940+
self._finishFetch(fetchedAttrs, true);
937941
return self;
938942
});
939943
},
940944

945+
_cleanupUnsetKeys(fetchedAttrs) {
946+
AV._objectEach(this._serverData, (value, key) => {
947+
if (fetchedAttrs[key] === undefined) delete this._serverData[key];
948+
});
949+
},
950+
941951
/**
942952
* Set a hash of model attributes, and save the model to the server.
943953
* updatedAt will be updated when the request returns.

test/object.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,16 @@ describe('Objects', function() {
384384
expect(score.createdAt).to.be.a(Date);
385385
expect(score.id).to.be.eql(gameScore.id);
386386
}));
387+
it('fetch should remove deleted keys', () => {
388+
const score = AV.parseJSON(
389+
Object.assign(gameScore.toFullJSON(), {
390+
fakedDeletedKey: 'value',
391+
})
392+
);
393+
return score.fetch().then(() => {
394+
expect(score.get('fakedDeletedKey')).to.eql(undefined);
395+
});
396+
});
387397
it('fetchAll', () =>
388398
AV.Object.fetchAll([
389399
AV.Object.createWithoutData('GameScore', gameScore.id),

0 commit comments

Comments
 (0)