Skip to content

Commit

Permalink
Fixed model attribute synchronization for update method
Browse files Browse the repository at this point in the history
closes #2007

- The attributes object can be modified within `triggerThen` through hooks. Sample scenario described here bookshelf/bookshelf#2007 (comment).
- Recalculating 'attributesToSave' object in after-triggerThen step solves the syncronization issue because it is still based on model's `attributes` property (the reference is not lost)
  • Loading branch information
techbc1221 committed Mar 6, 2020
1 parent a00d01a commit da0f1a4
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,9 @@ const BookshelfModel = ModelBase.extend(
Helpers.saveConstraints(this, this.relatedData);
}

const attributesToSave = method === 'update' && options.patch ? attrs : this.attributes;
const getAttributesToSave = function(method, options, model) {
return method === 'update' && options.patch ? attrs : model.attributes;
};

// Gives access to the `query` object in the `options`, in case we need it
// in any event handlers.
Expand Down Expand Up @@ -1150,12 +1152,12 @@ const BookshelfModel = ModelBase.extend(
return this.triggerThen(
method === 'insert' ? 'saving creating' : 'saving updating',
this,
attributesToSave,
getAttributesToSave(method, options, this),
options
)
.bind(this)
.then(function() {
return sync[options.method](attributesToSave);
return sync[options.method](getAttributesToSave(method, options, this));
})
.then(function(resp) {
// Only valid for databases that support RETURNING
Expand Down

0 comments on commit da0f1a4

Please sign in to comment.