diff --git a/lib/base/model.js b/lib/base/model.js index f35a4e98..770bacbf 100644 --- a/lib/base/model.js +++ b/lib/base/model.js @@ -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; }; diff --git a/test/integration/json.js b/test/integration/json.js index b8a87caa..da17b670 100644 --- a/test/integration/json.js +++ b/test/integration/json.js @@ -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,