Skip to content

Instantly share code, notes, and snippets.

@sktt
Last active September 4, 2017 01:17
Show Gist options
  • Save sktt/2a124426c5c750eade978b3f32ca9b40 to your computer and use it in GitHub Desktop.
Save sktt/2a124426c5c750eade978b3f32ca9b40 to your computer and use it in GitHub Desktop.
How to add support for pre updateItem hook for keystone.List.
// Naturally this has to be done before creating List instances!!
// monkey patching List constructor to add hooks support
(function () {
var grappling = require('grappling-hook');
var List = keystone.List;
keystone.List = function (name, options) {
List.apply(this, arguments);
grappling.mixin(this).allowHooks('pre:updateItem');
if (options && options.pre && options.pre.updateItem) {
this.pre('updateItem', options.pre.updateItem);
}
}
keystone.List.prototype = List.prototype;
})();
// monkey patching List#updateItem to call hook
(function () {
var updateItem = keystone.List.prototype.updateItem;
keystone.List.prototype.updateItem = function (item, data, options, callback) {
var self = this;
/* Process arguments and options */
if (typeof options === 'function') {
callback = options;
options = {};
}
if (!options) {
options = {};
}
this.callHook('pre:updateItem', item, data, options, function (err) {
if (err) {
return callback(err);
}
updateItem.call(self, item, data, options, callback);
})
}
})();
/// Now we can do this..
var MyModel = new Keystone.List('MyModel');
MyModel.pre('updateItem', function (item, data, options, next) {
// do something and then
next()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment