From aecca28257e158c52313cc9d5d9a72eeae84334e Mon Sep 17 00:00:00 2001 From: kirrg001 Date: Fri, 6 Apr 2018 19:10:59 +0200 Subject: [PATCH] Optimised `emitChange` for destroyed resources no issue - see comment in code base --- core/server/models/base/index.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/core/server/models/base/index.js b/core/server/models/base/index.js index 1b8edc263b..6ee2fe9ba6 100644 --- a/core/server/models/base/index.js +++ b/core/server/models/base/index.js @@ -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); } @@ -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; }); }