Added condition to not trigger events if nothing has changed

refs #9248

- we no longer trigger events if the db was not changed
This commit is contained in:
kirrg001 2019-02-02 20:18:27 +01:00
parent 3289dc7619
commit 8b3336c84d
2 changed files with 12 additions and 3 deletions

View File

@ -117,7 +117,12 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
*/
emitChange: function (model, event, options) {
if (!options.transacting) {
if (model._changed && !Object.keys(model._changed).length) {
return;
}
debug(`event trigger without txn: ${event}`);
return common.events.emit(event, model, options);
}
@ -137,6 +142,10 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
}
_.each(this.ghostEvents, (ghostEvent) => {
if (model._changed && !Object.keys(model._changed).length) {
return;
}
debug(`event: ${ghostEvent}`);
common.events.emit(ghostEvent, model, _.omit(options, 'transacting'));
});

View File

@ -449,7 +449,7 @@ describe('Post Model', function () {
});
}).then(function () {
// txn was successful
Object.keys(eventsTriggered).length.should.eql(6);
Object.keys(eventsTriggered).length.should.eql(4);
});
});
@ -708,8 +708,8 @@ describe('Post Model', function () {
should.exist(edited);
edited.attributes.status.should.equal('scheduled');
Object.keys(eventsTriggered).length.should.eql(1);
should.exist(eventsTriggered['post.edited']);
// nothing has changed
Object.keys(eventsTriggered).length.should.eql(0);
done();
}).catch(done);