2
1
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2023-12-13 21:00:40 +01:00

Simplified model event handler attachment

no-issue

This is smaller and easier to read than the previous approach
This commit is contained in:
Fabien O'Carroll 2019-08-14 10:32:45 +08:00
parent 24c8da58e4
commit e3c3633183

View file

@ -228,35 +228,18 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
} }
}); });
[ self.on('fetched', self.onFetched);
'fetching', self.on('fetching', self.onFetching);
'fetching:collection', self.on('fetched:collection', self.onFetchedCollection);
'fetched', self.on('fetching:collection', self.onFetchingCollection);
'fetched:collection', self.on('creating', self.onCreating);
'creating', self.on('created', self.onCreated);
'created', self.on('updating', self.onUpdating);
'updating', self.on('updated', self.onUpdated);
'updated', self.on('destroying', self.onDestroying);
'destroying', self.on('destroyed', self.onDestroyed);
'destroyed', self.on('saving', self.onSaving);
'saving', self.on('saved', self.onSaved);
'saved'
].forEach(function (eventName) {
var functionName = 'on' + eventName[0].toUpperCase() + eventName.slice(1);
if (functionName.indexOf(':') !== -1) {
functionName = functionName.slice(0, functionName.indexOf(':'))
+ functionName[functionName.indexOf(':') + 1].toUpperCase()
+ functionName.slice(functionName.indexOf(':') + 2);
functionName = functionName.replace(':', '');
}
if (!self[functionName]) {
return;
}
self.on(eventName, self[functionName]);
});
// @NOTE: Please keep here. If we don't initialize the parent, bookshelf-relations won't work. // @NOTE: Please keep here. If we don't initialize the parent, bookshelf-relations won't work.
proto.initialize.call(this); proto.initialize.call(this);
@ -271,6 +254,8 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
return validation.validateSchema(this.tableName, this, options); return validation.validateSchema(this.tableName, this, options);
}, },
onFetched() {},
/** /**
* http://knexjs.org/#Builder-forUpdate * http://knexjs.org/#Builder-forUpdate
* https://dev.mysql.com/doc/refman/5.7/en/innodb-locking-reads.html * https://dev.mysql.com/doc/refman/5.7/en/innodb-locking-reads.html
@ -284,19 +269,18 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
} }
}, },
onFetchedCollection() {},
onFetchingCollection: function onFetchingCollection(model, columns, options) { onFetchingCollection: function onFetchingCollection(model, columns, options) {
if (options.forUpdate && options.transacting) { if (options.forUpdate && options.transacting) {
options.query.forUpdate(); options.query.forUpdate();
} }
}, },
onSaving: function onSaving() { onCreated(model, attrs, options) {
// Remove any properties which don't belong on the model addAction(model, 'added', options);
this.attributes = this.pick(this.permittedAttributes());
}, },
onDestroying() {},
/** /**
* Adding resources implies setting these properties on the server side * Adding resources implies setting these properties on the server side
* - set `created_by` based on the context * - set `created_by` based on the context
@ -354,6 +338,10 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
}); });
}, },
onUpdated(model, attrs, options) {
addAction(model, 'edited', options);
},
/** /**
* Changing resources implies setting these properties on the server side * Changing resources implies setting these properties on the server side
* - set `updated_by` based on the context * - set `updated_by` based on the context
@ -408,13 +396,14 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
return Promise.resolve(this.onValidate(model, attr, options)); return Promise.resolve(this.onValidate(model, attr, options));
}, },
onCreated(model, attrs, options) { onSaved() {},
addAction(model, 'added', options);
onSaving: function onSaving() {
// Remove any properties which don't belong on the model
this.attributes = this.pick(this.permittedAttributes());
}, },
onUpdated(model, attrs, options) { onDestroying() {},
addAction(model, 'edited', options);
},
onDestroyed(model, options) { onDestroyed(model, options) {
if (!model._changed) { if (!model._changed) {
@ -429,8 +418,6 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
addAction(model, 'deleted', options); addAction(model, 'deleted', options);
}, },
onSaved() {},
/** /**
* before we insert dates into the database, we have to normalize * before we insert dates into the database, we have to normalize
* date format is now in each db the same * date format is now in each db the same