Library Reference
JS Library
Chains
8min
all chains are a list of callback functions that can be manipulated at any point in the execution as objects are sent as a reference to a function we utilize this core js functionality to achieve the desired effect klevu framework uses chains for control the flow of a search instance – check search instance scope for the predefined chains control of the core loading events – refer to klevu events docs custom event executions – refer to klevu events docs understanding chains the concept of chaining is derived from the promise and callback concepts in js with some differences the klevu chains where designed to work in same way callbacks work in that they are executed at a specific point in the code and will loop through a list of previously defined functions depending on the settings used at the time of creation of the chain any function in that chain can stop execution of future ones and return the edited result they are mainly used to manipulate a data object that is attached to a specific scope, both of which are being passed as object references the main between callbacks and chains is that , because the chains are mainly use to manipulate data the order functions that make up that chain are executed is important and so the chains will have the possibility to change the order, remove or add functions at any point in the execution chain example of a chain and what can be done with the use of interfaces we have function a , b , c , d that have to be executed in that order and are added to the chain that was created , there execution will be as follows a | b | c | d if we insert e with the addbefore b we will get an execution of a | e | b | c | d if we insert f with the add we will get an execution of a | b | c | d | f this allows for grater flexibility in the order of execution and more granulation of the code using chains to utilize a chain first it has to be created , to achieve this the following core interface has to be used var chain = klevu chain(\<options>); options that can be set on the chain stoponfalse – true/false default false – the chain execution will stop when any element in the chain returns false spacer – numeric, default 10, the distance between the automatically added elements interfaces add (chainelement) – adds a new element to the chain at the end of it addafter (name , chainelement) – adds a new element after the element with a specific name addbefore (name , chainelement) – adds a new element before the element with a specific name move – responsible for the moving of an element , accepts an object with following attributes name – required – the name of the element to move before – optional – if element has to be moved before a specific element after – optional – if element has to be moved after a specific element list – retrieve a list of all the existing elements in the chain hasdata – checks if data object is set getdata – returns data object setdata – sets new data object – this has to always be an object type hasscope – checks if scope object is set getscope – returns scope object setscope – sets new scope object – this has to always be an object type getoptions – retrieve core options and declarations of the chain elements chain elements are attached to chains these elements are the callback functions to be executed some extra options allow the modification of order the callback functions have at the time of execution a chain element has the following possible parameters name \<str> – defines name of the element, it has to be unique per chain position \<numeric> – optional , defines the position of the element fire \<function source> – defines what needs to be executed, accepts 2 variables, data and scope of the chain box getscope() chains request build add({ name "klevuexamplechain", fire function (data, scope) { } });