Gravatar for dan@getfishtank.ca

Question by Dan Cruickshank, May 16, 2014 11:54 AM

With multiple JS Search instances on one page, what is the best way to prevent each box from executing a query?

To support responsive design, there are multiple search boxes on a page. The ResultList component is only used by #main-search instance of Coveo on the page.

The two other search boxes use TopFieldSuggestion component but are implemented as Standalone/Global Search Box from the April release (https://developers.coveo.com/display/JsSearch/Standalone+Search+Box - it looks like formal implementation of the concept is in the May release)

  var searchInterface = Coveo.$("#searchbox");
        searchInterface.on("afterInitialization", function () {
            searchInterface.on("newQuery", function (e, args) {
                // DC - Commented line below, see: https://answers.coveo.com/questions/113/when-keyboard-is-visible-on-ios-6-7-clicking-coveosearchbutton-does-not-execute-search
                // var currentKeyword = searchInterface.coveo("state", "q");
                var currentKeyword = Coveo.$.trim(Coveo.$('.CoveoQueryBox').val());
                if (currentKeyword != "") {
                    window.location.href = searchPage + "#q=" + encodeURIComponent(currentKeyword);
                }
            });

        });

I've disabled history on the #searchbox - the HistoryController get's tripped-up setting the hash with multiple Coveo instances on a single page.

With the code above the hash get's set & the #main-search on the page (with History enabled) fires the query. But the problem is that after #searchbox sets the hash it also fires a query, logs analytics and returns a results object.

As of now - To prevent this, I'm leaning towards patching the QueryController.ExecuteQuery of that particular #searchbox so it fires a "newQuery" event but does little else.

Anyone have any better ideas ?

1 Reply
Gravatar for mlaporte@coveo.com

Answer by Martin Laporte, May 16, 2014 12:01 PM

I think patching executeQuery in this case is appropriate. To save on complexity you could do the redirect directly in the patched method instead of raising a newQuery that you then trap.

The new standalone search box feature does indeed adresses this exact issue by doing something very similar (executeQuery only redirects to the search page instead of triggering a query.

Gravatar for dan@getfishtank.ca

Comment by Dan Cruickshank, May 16, 2014 3:58 PM

Thank you Martin. That is exactly what I'll do then. I wasn't sure if there any value in firing "newQuery" or not.

Ask a question