Skip to content

Commit

Permalink
Merge pull request #260 from davincho/master
Browse files Browse the repository at this point in the history
closes #259 Inject reverted object with option 'replace'
  • Loading branch information
jmdobry committed Nov 3, 2015
2 parents 0633d01 + a4dc250 commit dbf2022
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/datastore/sync_methods/index.js
Expand Up @@ -386,13 +386,13 @@ export default {
let {_this, definition, _resourceName, _id, _options} = check.call(this, 'revert', resourceName, id, options)

let preserve = _options.preserve || []
let injectObj = {}

if (preserve.length === 0) {
return definition.inject(_this.previous(_resourceName, _id))
injectObj = _this.previous(_resourceName, _id)
} else {
let instance = definition.get(id)
let previousInstance = _this.previous(_resourceName, _id)
let injectObj = {}

if (!instance) { return }

Expand All @@ -403,8 +403,10 @@ export default {
injectObj[key] = previousInstance[key]
}
})

return definition.inject(injectObj)
}

return definition.inject(injectObj, {
onConflict: 'replace'
})
}
}
8 changes: 8 additions & 0 deletions test/both/datastore/sync_methods/revert.test.js
Expand Up @@ -29,4 +29,12 @@ describe('DS#revert', function () {
assert.equal(post.age, 20, 'The age of the post should have been preserved');
assert.equal(post.author, 'John', 'The author of the post should have been reverted');
});
it('should revert key which has not been injected', function() {
Post.inject(p1);
var post = Post.get(p1.id);
assert.isUndefined(post.newProperty);
post.newProperty = 'new Property';
post.DSRevert();
assert.isUndefined(post.newProperty);
});
});

0 comments on commit dbf2022

Please sign in to comment.