Klevu Chains

Klevu Chains

Description:  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.

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 thrue 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:

If we insert E with the addBefore B we will get an execution of

If we insert F with the add we will get an execution of

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 – defines if the chain execution will stop when any element in the chain returns false
  • spacer – numeric, default 10, the distance between the automatically added elements


  • 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

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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>