File tree Expand file tree Collapse file tree 4 files changed +46
-7
lines changed
src/datastore/sync_methods
test/both/datastore/sync_methods Expand file tree Collapse file tree 4 files changed +46
-7
lines changed Original file line number Diff line number Diff line change 12
12
"url" : " http://www.pseudobry.com" ,
13
13
"email" : " jason.dobry@gmail.com"
14
14
},
15
- "licenses" : [
16
- {
17
- "type" : " MIT" ,
18
- "url" : " https://github.com/js-data/js-data/blob/master/LICENSE"
19
- }
20
- ],
15
+ "license" : " MIT" ,
21
16
"main" : " ./dist/js-data-debug.js" ,
22
17
"keywords" : [
23
18
" orm" ,
Original file line number Diff line number Diff line change @@ -26,7 +26,8 @@ let instanceMethods = [
26
26
'hasChanges' ,
27
27
'lastModified' ,
28
28
'lastSaved' ,
29
- 'previous'
29
+ 'previous' ,
30
+ 'revert'
30
31
] ;
31
32
32
33
export default function defineResource ( definition ) {
Original file line number Diff line number Diff line change @@ -420,5 +420,27 @@ export default {
420
420
421
421
// return resource from cache
422
422
return resource . previousAttributes [ id ] ? DSUtils . copy ( resource . previousAttributes [ id ] ) : undefined ;
423
+ } ,
424
+ /**
425
+ * Revert all attributes of the item with the given primary key to their previous values.
426
+ *
427
+ * @param resourceName The name of the type of resource of the item.
428
+ * @param id The primary key of the item.
429
+ * @returns The reverted item
430
+ */
431
+ revert ( resourceName , id ) {
432
+ let _this = this ;
433
+ let definition = _this . defs [ resourceName ] ;
434
+
435
+ id = DSUtils . resolveId ( definition , id ) ;
436
+ if ( ! definition ) {
437
+ throw new NER ( resourceName ) ;
438
+ } else if ( ! DSUtils . _sn ( id ) ) {
439
+ throw DSUtils . _snErr ( 'id' ) ;
440
+ }
441
+
442
+ definition . logFn ( 'revert' , id ) ;
443
+
444
+ return definition . inject ( _this . previous ( resourceName , id ) ) ;
423
445
}
424
446
} ;
Original file line number Diff line number Diff line change
1
+ describe ( 'DS#revert' , function ( ) {
2
+ it ( 'should throw an error when method pre-conditions are not met' , function ( ) {
3
+ assert . throws ( function ( ) {
4
+ store . previous ( 'does not exist' , { } ) ;
5
+ } , store . errors . NonexistentResourceError , 'does not exist is not a registered resource!' ) ;
6
+
7
+ DSUtils . forEach ( TYPES_EXCEPT_STRING_OR_NUMBER , function ( key ) {
8
+ assert . throws ( function ( ) {
9
+ store . previous ( 'post' , key ) ;
10
+ } , store . errors . IllegalArgumentError , '"id" must be a string or a number!' ) ;
11
+ } ) ;
12
+ } ) ;
13
+ it ( 'should return the previous version of an item' , function ( ) {
14
+
15
+ Post . inject ( p1 ) ;
16
+ var post = Post . get ( 5 ) ;
17
+ post . author = 'Jake' ;
18
+ post . DSRevert ( ) ;
19
+ assert . equal ( JSON . stringify ( post ) , JSON . stringify ( p1 ) ) ;
20
+ } ) ;
21
+ } ) ;
You can’t perform that action at this time.
10CF
div>
0
0 commit comments