File tree Expand file tree Collapse file tree 3 files changed +49
-0
lines changed
Expand file tree Collapse file tree 3 files changed +49
-0
lines changed Original file line number Diff line number Diff 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.
Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff 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' ) ;
You can’t perform that action at this time.
0 commit comments