Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create previousAttributes using a deep clone of attributes #1876

Merged
merged 1 commit into from Nov 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this description fits with what is actually being tested here. It should be the opposite since previousAttributes will now detect changes to nested objects.

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) {
Expand Down