Skip to content

Commit 6c00bf6

Browse files
committed
feat(Object): add #revert()
1 parent 9788b86 commit 6c00bf6

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

src/object.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,20 @@ module.exports = function(AV) {
897897
return this.set(keysToClear, options);
898898
},
899899

900+
/**
901+
* Clears any (or specific) changes to the model made since the last save.
902+
* @param {string|string[]} [keys] specify keys to revert.
903+
*/
904+
revert(keys) {
905+
const lastOp = _.last(this._opSetQueue);
906+
const _keys = ensureArray(keys || _.keys(lastOp));
907+
_keys.forEach(key => {
908+
delete lastOp[key];
909+
});
910+
this._rebuildAllEstimatedData();
911+
return this;
912+
},
913+
900914
/**
901915
* Returns a JSON-encoded set of operations to be sent with the next save
902916
* request.

storage.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ export class Object extends BaseObject {
303303
change(options: any): this;
304304
changedAttributes(diff: any): boolean;
305305
clear(options: any): any;
306+
revert(keys?: string | string[]): this;
306307
clone(): this;
307308
destroy(options?: Object.DestroyOptions): Promise<this>;
308309
dirty(attr: String): boolean;

test/object.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,40 @@ describe('Objects', function() {
322322
});
323323
});
324324

325+
describe('revert', () => {
326+
const data = {
327+
name: 'AVOSCloud',
328+
age: 0,
329+
objectId: '11111111111',
330+
className: 'Test',
331+
__type: 'Object',
332+
};
333+
334+
it('revert all', () => {
335+
const object = AV.parseJSON(data);
336+
object.set({
337+
name: 'LeanCoud',
338+
age: 1,
339+
});
340+
object.revert();
341+
object.dirty().should.eql(false);
342+
object.get('name').should.eql('AVOSCloud');
343+
object.get('age').should.eql(0);
344+
object.toFullJSON().should.eql(data);
345+
});
346+
it('revert keys', () => {
347+
const object = AV.parseJSON(data);
348+
object.set({
349+
name: 'LeanCoud',
350+
age: 1,
351+
});
352+
object.revert('name');
353+
object.dirty().should.eql(true);
354+
object.get('name').should.eql('AVOSCloud');
355+
object.get('age').should.eql(1);
356+
});
357+
});
358+
325359
describe('query', () => {
326360
it('should get AV.Query instance', () => {
327361
const Foo = AV.Object.extend('Foo');

0 commit comments

Comments
 (0)