Mixin: GrapplingHook

GrapplingHook

Grappling hook

Source:

Methods

addAsyncHooks()

Since:
  • 3.0.0
Source:

addHooks(methods) → {GrapplingHook}

Wraps asynchronous methods/functions with pre and/or post hooks

Parameters:
Name Type Description
methods String | Array.<String> | Object | Array.<Object>

method(s) that need(s) to emit pre and post events

Source:
See:
Returns:
Type
GrapplingHook
Examples
//wrap existing methods
instance.addHooks('save', 'pre:remove');
//add method and wrap it
instance.addHooks({
  save: instance._upload,
  "pre:remove": function(){
  	//...
  }
});

addSyncHooks(methods) → {GrapplingHook}

Wraps synchronous methods/functions with pre and/or post hooks

Parameters:
Name Type Description
methods String | Array.<String> | Object | Array.<Object>

method(s) that need(s) to emit pre and post events

Since:
  • 2.4.0
Source:
See:
Returns:
Type
GrapplingHook

addThenableHooks(methods) → {GrapplingHook}

Wraps thenable methods/functions with pre and/or post hooks

Parameters:
Name Type Description
methods String | Array.<String> | Object | Array.<Object>

method(s) that need(s) to emit pre and post events

Since:
  • 3.0.0
Source:
See:
Returns:
Type
GrapplingHook

allowHooks(hooks) → {GrapplingHook}

Explicitly declare hooks

Parameters:
Name Type Description
hooks string | Array.<string>

(qualified) hooks e.g. pre:save or save

Source:
Returns:
Type
GrapplingHook

callAsyncHook()

Since:
  • 3.0.0
Source:

callHook(contextopt, qualifiedHook, …parametersopt, callbackopt) → {GrapplingHook}

Calls all middleware subscribed to the asynchronous qualifiedHook and passes remaining parameters to them

Parameters:
Name Type Attributes Description
context * <optional>

the context in which the middleware will be called

qualifiedHook String

qualified hook e.g. pre:save

parameters * <optional>
<repeatable>

any parameters you wish to pass to the middleware.

callback function <optional>

will be called when all middleware have finished

Source:
See:
Returns:
Type
GrapplingHook

callSyncHook(contextopt, qualifiedHook, …parametersopt) → {GrapplingHook}

Calls all middleware subscribed to the synchronous qualifiedHook and passes remaining parameters to them

Parameters:
Name Type Attributes Description
context * <optional>

the context in which the middleware will be called

qualifiedHook String

qualified hook e.g. pre:save

parameters * <optional>
<repeatable>

any parameters you wish to pass to the middleware.

Since:
  • 2.4.0
Source:
See:
Returns:
Type
GrapplingHook

callThenableHook(contextopt, qualifiedHook, …parametersopt) → {thenable}

Calls all middleware subscribed to the synchronous qualifiedHook and passes remaining parameters to them

Parameters:
Name Type Attributes Description
context * <optional>

the context in which the middleware will be called

qualifiedHook String

qualified hook e.g. pre:save

parameters * <optional>
<repeatable>

any parameters you wish to pass to the middleware.

Since:
  • 3.0.0
Source:
See:
Returns:
  • a thenable, as created with options.createThenable
Type
thenable

getMiddleware(qualifiedHook) → {Array.<middleware>}

Retrieve all middleware registered to qualifiedHook

Parameters:
Name Type Description
qualifiedHook

qualified hook, e.g. pre:save

Source:
Returns:
Type
Array.<middleware>

hasMiddleware(qualifiedHook) → {boolean}

Determines whether any middleware is registered to qualifiedHook.

Parameters:
Name Type Description
qualifiedHook string

qualified hook, e.g. pre:save

Source:
Returns:
Type
boolean

hook(qualifiedHook, middleware) → {GrapplingHook|thenable}

Adds middleware to a qualified hook. Convenience method which allows you to add middleware dynamically more easily.

Parameters:
Name Type Description
qualifiedHook String

qualified hook e.g. pre:save

middleware middleware | Array.<middleware>

middleware to call

Source:
Returns:
Type
GrapplingHook | thenable
Example
instance.hook('pre:save', function(next) {
  console.log('before saving');
  next();
}

hookable(qualifiedHook) → {boolean}

Determines whether registration of middleware to qualifiedHook is allowed. (Always returns true for lenient instances)

Parameters:
Name Type Description
qualifiedHook String | Array.<String>

qualified hook e.g. pre:save

Source:
Returns:
Type
boolean

post(hook, middlewareopt) → {GrapplingHook|thenable}

Registers middleware to be executed after hook. This is a dynamically added method, that may not be present if otherwise configured in options.qualifiers.

Parameters:
Name Type Attributes Description
hook string

hook name, e.g. 'save'

middleware middleware | Array.<middleware> <optional>

middleware to register

Source:
See:
Returns:

the GrapplingHook instance itself, or a thenable if no middleware was provided.

Type
GrapplingHook | thenable
Example
instance.post('save', function(){
  console.log('after saving');
});

pre(hook, middlewareopt) → {GrapplingHook|thenable}

Registers middleware to be executed before hook. This is a dynamically added method, that may not be present if otherwise configured in options.qualifiers.

Parameters:
Name Type Attributes Description
hook string

hook name, e.g. 'save'

middleware middleware | Array.<middleware> <optional>

middleware to register

Source:
See:
Returns:

the GrapplingHook instance itself, or a thenable if no middleware was provided.

Type
GrapplingHook | thenable
Example
instance.pre('save', function(){
  console.log('before saving');
});

unhook(hookopt, middlewareopt) → {GrapplingHook}

Removes middleware for hook

Parameters:
Name Type Attributes Description
hook String <optional>

(qualified) hooks e.g. pre:save or save

middleware middleware | Array.<middleware> <optional>

function(s) to be removed

Source:
Returns:
Type
GrapplingHook
Examples
//removes `onPreSave` Function as a `pre:save` middleware
instance.unhook('pre:save', onPreSave);
//removes all middleware for `pre:save`
instance.unhook('pre:save');
//removes all middleware for `pre:save` and `post:save`
instance.unhook('save');
//removes ALL middleware
instance.unhook();