Skip to content

Commit

Permalink
Optimised emitChange for destroyed resources
Browse files Browse the repository at this point in the history
no issue

- see comment in code base
  • Loading branch information
kirrg001 committed Apr 6, 2018
1 parent fb79f24 commit aecca28
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions core/server/models/base/index.js
Expand Up @@ -101,7 +101,15 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
return [];
},

/**
* @NOTE
* We have to remember the `_previousAttributes` attributes, because when destroying resources
* We listen on the `onDestroyed` event and Bookshelf resets these properties right after the event.
* If the query runs in a txn, `_previousAttributes` will be empty.
*/
emitChange: function (model, event, options) {
const previousAttributes = model._previousAttributes;

if (!options.transacting) {
return common.events.emit(event, model, options);
}
Expand All @@ -119,8 +127,11 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
}

_.each(this.ghostEvents, (ghostEvent) => {
model._previousAttributes = previousAttributes;
common.events.emit(ghostEvent, model, _.omit(options, 'transacting'));
});

delete model.ghostEvents;
});
}

Expand Down

0 comments on commit aecca28

Please sign in to comment.