hook - PeterNaydenov/code-assembly-line GitHub Wiki
Hook functions in Code-Assembly-Line
are providing a program-logic for render-processes. Having a logicless templates in Code-Assembly-Line doesn't mean that we don't need logic at all. Hooks are coming as a separate program component that can be different for each render-process execution.
Associate hook functions and process during the process execution run. Every single execution can have different hook function. Hook names are the same in order to describe their role inside the render process:
// * Hook function
function hookFunction ( currentData ) {
// Manipulate data and return it...
return currentData
}
// * Provide hook functions during execution of the process:
tplEngine.run ( 'processName', data, { hookName: hookFunction })
Hook can be described as a separate process-step and will look like this:
{ do: 'hook', name: 'hookName' }
Find available hook names by requesting tplEngine.getHooks ( processList )
where processList is an array of process names. Function will return object with all hookNames where hook names look like this: 'processName/hookname'.
Hook functions are not used only from hook
step. They are used also from draw
step by using hook and watchHook parameters. Find more details about this in draw step description.
Fields
{
do: 'hook'
, name: hookName
}
Types
- hookName: string. Hook function name that should be executed. It's the key in the hook object.
Example
const
process = [ // define simple process with 'hook' step in it
{ do: 'draw', tpl: 'simple' }
, { do: 'hook', name: 'afterSimple' }
, { do: 'block', name: 'result' }
]
, simpleTemplate = 'Just simple text, {{name}}!'
;
function simpleHook (current ) {
// current is the result of process till current moment. Return next 'current' as result of hook function.
return current.push ( 'Extension from the hook.' )
} // simpleHook
const tplEngine = new CodeAssemblyLine();
tplEngine.insertProcess ( process, 'hookProcess' )
tplEngine.insertTemplate ({simple:simpleTemplate})
tplEngine.run ( 'hookProcess', { name: 'Johny' }, {afterSimple:simpleHook}) // provide hooks on running process
const result = tpl.getBlock ( 'result' )
// result > Just simple text, Johny! Extension from the hook
// TODO: Runkit playground