10000 add revert, closes #173 · StudyForFun/js-data@515ce5d · GitHub
[go: up one dir, main page]

Skip to content

Commit 515ce5d

Browse files
committed
add revert, closes js-data#173
1 parent 1e1fd64 commit 515ce5d

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

package.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@
1212
"url": "http://www.pseudobry.com",
1313
"email": "jason.dobry@gmail.com"
1414
},
15-
"licenses": [
16-
{
17-
"type": "MIT",
18-
"url": "https://github.com/js-data/js-data/blob/master/LICENSE"
19-
}
20-
],
15+
"license": "MIT",
2116
"main": "./dist/js-data-debug.js",
2217
"keywords": [
2318
"orm",

src/datastore/sync_methods/defineResource.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ let instanceMethods = [
2626
'hasChanges',
2727
'lastModified',
2828
'lastSaved',
29-
'previous'
29+
'previous',
30+
'revert'
3031
];
3132

3233
export default function defineResource(definition) {

src/datastore/sync_methods/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,5 +420,27 @@ export default {
420420

421421
// return resource from cache
422422
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));
423445
}
424446
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
});

0 commit comments

Comments
 (0)
0