Interacting With the Framework and Its Components

There are three distinct but equivalent ways to interact with the Coveo JavaScript Search Framework and its components.

About Lazy Loading

Coveo JavaScript Search Framework 2.2900.23 (July 2017)

As of its July 2017 Release (v2.2900.23), the Coveo JavaScript Search Framework allows you to use lazy loading to asynchronously download only the minimal amount of code your search page requires, rather than using eager loading to sequentially download the entire framework, whether your search page needs the totality of this code or not.

When using lazy loading in your search page, you need to wait for a component promise to resolve before you can interact with it.

Interacting with a lazy Tab component would look something like this:

Coveo.load('Tab').then(function(Tab) {
  // Interact with the Tab component.
});

For more information on how to interact with the Coveo JavaScript Search Framework using lazy loading, see Lazy Versus Eager Component Loading - Interacting with Lazy Components.

For the sake of simplicity, this page uses eager loading in its examples, which does not require you to wait for component promises to resolve.

Using “Pure” JavaScript

You can interact with the Coveo JavaScript Search Framework using standard JavaScript.

Getting a Component Instance

You can use standard JavaScript methods such as document.querySelector() to get the HTML element of a component you want to interact with.

The Coveo.get() method then allows you to get a reference to the corresponding component instance. You can call methods from this reference.

Consider the following markup:

[ ... ]
 
<div class='coveo-facet-column'>
 
  [ ... ]
 
  <div class='CoveoFacet' data-field='@author'></div>
 
  [ ... ]
</div>
[ ... ]

Running the following code sample will select the Facet component instance with the field option set to @author and collapse it (see Facet.collapse()).

var myFacetElement = document.querySelector('.CoveoFacet[data-field="@author"]');
var myFacetInstance = Coveo.get(myFacetElement);
myFacetInstance.collapse();

Calling a Top-Level Function

Top-level functions are functions you can call directly from the Coveo namespace (e.g., Coveo.executeQuery(), Coveo.state(), etc.). These functions usually require the root element of your search interface as an argument.

In most scenarios, this root element of your search interface has the search id.

Consider the following markup:

[ ... ]
 
<body id='search' class='CoveoSearchInterface'>
 
[ ... ]
 
</body>

Running the following code sample will execute a query (see Coveo.executeQuery()).

var mySearchInterfaceRootElement = document.querySelector('#search');
Coveo.executeQuery(mySearchInterfaceRootElement);

Binding an Event Handler

You can use the standard JavaScript EventTarget.addEventListener() method to bind event handlers. Most of the time, the event target should be the root element of your search interface.

A native event handler receives the event argument in the detail property of the CustomEvent instead of a second argument, as is the case when binding event handlers using either Coveo.$$ or jQuery.

The script part of the following code sample binds an event handler to the root element of the search interface on the buildingQuery event to add foobar to the current query expression.

<head>
 
  [ ... ]
 
  <script>
    document.addEventListener('DOMContentLoaded', function () {
      Coveo.SearchEndpoint.configureSampleEndpoint();
      var root = document.querySelector('#search');  
      root.addEventListener('buildingQuery', function(e) {
        e.detail.queryBuilder.expression.add('foobar');
      });
      Coveo.init(root);
    })
  </script>
 
  [ ... ]
 
</head>
 
<body id='search' class='CoveoSearchInterface'>
  [ ... ]
 
</body>

Using Dom Helper Class (Coveo.$$)

You can use the Coveo.$$ function, which wraps the Dom class to interact with the Coveo JavaScript Search Framework and its components. The Dom class implements a small subset of jQuery functionalities that the Coveo JavaScript Search Framework requires (append, find, on, etc.).

The Dom class was introduced to minimize the risk of having jQuery conflicts when integrating the Coveo JavaScript Search Framework in various systems.

Internally, the Coveo JavaScript Search Framework does not use jQuery at all; it always uses the Dom class.

Getting a Component Instance

You can use the Dom.find() method to get the HTML element of a component you want to interact with. The Coveo.get() method then allows you to get a reference to the corresponding component instance. You can call methods from this reference.

Consider the following markup:

[ ... ]
 
<div class='coveo-tab-section'>
 
[ ... ]
 
  <div class='CoveoTab' data-id='All'></div>
 
[ ... ]
</div>
[ ... ]

Running the following code sample selects the Tab component instance whose id option is All and selects it (seeTab.select()).

var myTabElement = Coveo.$$(document).find('.CoveoTab[data-id="All"]');
var myTabInstance = Coveo.get(myTabElement);
myTabInstance.select();

Calling a Top-Level Function

Top-level functions are functions you can call directly from the Coveo namespace (e.g., Coveo.executeQuery(), Coveo.state(), etc.). These functions usually require the root element of your search interface as an argument.

In most scenarios, this root element of your search interface has the search id.

If you want to get this element with Coveo.$$, you could use the Dom.find() method.

Consider the following markup:

[ ... ]
 
<body id='search' class='CoveoSearchInterface'>
 
[ ... ]
 
</body>

Running the following code sample sets the current sort criteria of the QueryStateModel to date ascending (see Coveo.state()).

var mySearchInterfaceRootElement = Coveo.$$(document).find('#search');
Coveo.state(mySearchInterfaceRootElement, 'sort', 'date ascending');

Binding an Event Handler

You can use the Dom.on() method to bind event handlers. Most of the time, the event target should be the root element of your search interface.

The script part of the following code sample binds an event handler to the root element of the search interface on the buildingQuery event to add foobar to the current query expression.

<head>
  [ ... ]
  <script>
    document.addEventListener('DOMContentLoaded', function () {
      Coveo.SearchEndpoint.configureSampleEndpoint();
      var root = Coveo.$$(document).find('#search');  
      Coveo.$$(root).on('buildingQuery', function(e, args) {
        args.queryBuilder.expression.add('foobar');
      });
      Coveo.init(root);
    })
  </script>
 
  [ ... ]
</head>
 
<body id='search' class='CoveoSearchInterface'>
  [ ... ]
</body>

Using the jQuery Extension

You can use the jQuery extension to call any of the Coveo JavaScript Search Framework top-level functions and component methods (see jQuery).

If you are unfamiliar with jQuery, you are encouraged to use another method instead.

Initially, the Coveo JavaScript Search Framework was designed primarily as a jQuery extension. Over time, however, the Dom class was introduced to eliminate the need for this dependency.

The jQuery extension is thus no longer considered “core” to the Coveo JavaScript Search Framework. This implies that you have to manually add a reference to the jQuery library if you want to be able to use the jQuery extension.

You could include an online reference to the jQuery library.

<head>
 
  [ ... ]
 
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
 
  [ ... ]
 
</head>

Some Coveo implementations such as Coveo for Salesforce and Coveo for Sitecore currently include their own references the jQuery library. Therefore, the jQuery extension should work out of the box with these products and you normally do not need to add a new reference to the jQuery library to use it (see Coveo for Salesforce V2 and Coveo for Sitecore 4 Release Home).

Getting a Component Instance

You can call the Coveo.get() top-level function with the jQuery extension to get a component instance.

Consider the following markup:

[ ... ]
 
<div class='coveo-search-section'>
 
  [ ... ]
 
  <div class='CoveoSettings'></div>
 
  [ ... ]
 
[ ... ]

Running the following code sample puts a reference to the Settings component instance in the mySettingsComponentInstance variable.

var mySettingsComponentInstance = $('.CoveoSettings').coveo('get');

Using the jQuery extension, you can also call a method directly on a component instance.

Consider the following markup:

[ ... ]
 
<div class='coveo-facet-column'>
 
  [ ... ]
 
  <div class='CoveoFacetSlider' data-field='@size'></div>
 
  [ ... ]
 
[ ... ]

Running this code sample puts a copy of the current selection of the FacetSlider component instance whose id option is @size in the currentSelection variable (see FacetSlider.getSelectedValues()).

var currentSelection = $('.CoveoFacetSlider[data-id="@size"]').coveo('getSelectedValues');

If a method requires parameters, you can pass them as arguments with the jQuery extension.

Consider the following markup:

[ ... ]
<div class='coveo-facet-column'>
  [ ... ]
  <div class='CoveoFacetSlider' data-field='@size' data-range-slider='true' data-begin='0' data-end='100'></div>
  [ ... ]
[ ... ]

Running this code sample sets the currently selected values of the FacetSlider component instance whose id option is @size to 10 and 25 (see FacetSlider.setSelectedValues()).

$('.CoveoFacetSlider[data-id="@size"]').coveo('setSelectedValues, [10, 25]);

Calling a Top-Level Function

Top-level functions are functions you can call directly from the Coveo namespace (e.g., Coveo.executeQuery(), Coveo.state(), etc.). You can call any top-level function using the jQuery extension. If a function requires parameters, you can pass them as arguments with the jQuery extension.

Consider the following markup:

[ ... ]
<body id='search' class='CoveoSearchInterface'>
[ ... ]
</body>

Running the following code sample will log a custom event to the Coveo Search Analytics service (see Coveo.logCustomEvent()).

var myCustomEventReason = { name : 'myCustomEventName', type : 'myCustomEventType' };
var myCustomEventMetadata = { myKey_1 : 'my value 1', myKey_2 : 'my value 2', myKey_n : 'my value n' };
$('#search').coveo('logCustomEvent', myCustomEventReason, myCustomEventMetadata);

Binding an Event Handler

While this is not strictly part of the Coveo jQuery extension, you can use the jQuery on() method to bind an event handler. Most of the time, the event target should be the root element of your search interface.

The script part of the following code sample binds an event handler to the root element of the search interface on the buildingQuery event to add foobar to the current query expression.

<head>
  [ ... ]
  <script>
    document.addEventListener('DOMContentLoaded', function () {
      Coveo.SearchEndpoint.configureSampleEndpointV2();
      var root = $('#search'); 
      $(root).on('buildingQuery', function(e, args) {
        args.queryBuilder.expression.add('foobar');
      });
      Coveo.init(root);
    })
  </script>
 
  [ ... ]
</head>
 
<body id='search' class='CoveoSearchInterface'>
  [ ... ]
</body>

Which Way Should I Use?

You can use any of the three preceding ways to achieve perfectly equivalent results. It is up to you to decide which one you prefer.

  • Using “pure” JavaScript can be a little verbose, but it will obviously always be supported.
  • Using Coveo.$$ allows you to access the Dom class, which implements some of the most frequently used jQuery functionalities without actually depending on jQuery at all. Internally, this is the method that the Coveo JavaScript Search Framework uses.
  • Using the jQuery extension can allow you to write less verbose code. However, it is no longer considered “core” to the Coveo JavaScript Search Framework. The current major version of the framework will always support it, though.