From 0d35119f26089e1785fe5db27c9738889ceba3db Mon Sep 17 00:00:00 2001 From: Julian Okuyiga Date: Sun, 8 Jul 2018 14:13:46 -0700 Subject: [PATCH] Create previousAttributes using a deep clone of attributes --- lib/base/model.js | 2 +- test/integration/json.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/base/model.js b/lib/base/model.js index 3a6f29c5..ac12b22f 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 4e9e6926..1685c70e 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, type: 'attack', info: {test: 'blah'}}).fetch() .then(function(command) {