Middleware like hooks for node.js
Middleware like hooks for node.js which is useful for building some relations between entities (ODM, ORM, etc).
npm install mhook
Define your actions, bind hooks on them, trigger actions. When you trigger an action all binded hooks will be sequentially executed.
var Hook = require'mhook'Hook;var hook = 'beforeUpdate' 'afterUpdate' 'afterRemove';// add some hookhookon'beforeUpdate'// do something, and then say that you done,// pass error as first argument to donedone;;// add another hookhookon'beforeUpdate'done;;// trigger `action` - executes all two hookshooktrigger'beforeUpdate'// this function will be called after all// hooks done or one of them fail;
trigger methods to your object you can inherits from
var Hook = require'mhook'Hookinherits = require'util'inherits;// apply parent cunstructorHookcallthis 'beforeUpdate' 'afterUpdate' 'afterRemove';// inherits from HookinheritsModel Hook;// now we can use `on` and `trigger` as own methodsthistrigger'beforeUpdate' objif err callbackerr; return// update// trigger afterUpdate, etc;;var model = ;modelon'beforeUpdate'console.log'before update object: ' obj;done;;
accepts array of string
actions - possible actions which could be used
hookArgs (arguments which will be passed to
every hook function).
callback (accepts error as first argument) function presents it will be
called after hooks execution.
into cloned repository run