Skip to content

Commit

Permalink
Merge pull request #1876 from okuyiga/feature/fix-nested-json-bug
Browse files Browse the repository at this point in the history
Create previousAttributes using a deep clone of attributes
  • Loading branch information
ricardograca committed Nov 4, 2018
2 parents 8259d71 + 0d35119 commit 32add1b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/base/model.js
Expand Up @@ -716,7 +716,7 @@ ModelBase.prototype.previousAttributes = function() {
* @returns {Model} This model.
*/
ModelBase.prototype._reset = function() {
this._previousAttributes = _.clone(this.attributes);
this._previousAttributes = _.cloneDeep(this.attributes);
this.changed = Object.create(null);
return this;
};
Expand Down
14 changes: 14 additions & 0 deletions test/integration/json.js
Expand Up @@ -52,6 +52,20 @@ module.exports = function(bookshelf) {
});
});

it('can update attributes without affecting _previousAttributes', function() {
return Command.forge({id: 0}).fetch()
.then(function(command) {
const newTarget = {
x: 7,
y: 13,
};
const updatedInfo = command.get('info');
updatedInfo.target = newTarget;
command.set('info', updatedInfo);
expect(command.get('info')).to.not.deep.eql(command.previous('info'));
});
});

it('Trying to fetch a model automatically excludes JSON column', function() {
return Command.forge({
unit_id: 1,
Expand Down

0 comments on commit 32add1b

Please sign in to comment.