Gravatar for hernandez@rdacorp.com

Question by benaldo, Mar 6, 2017 12:02 PM

Cancel query in newQuery state and redirect user

Hi,

I'm having an issue with a query cancel and redirect during the 'newQuery' event. Here's the code:

Coveo.$('#search').on('newQuery', function(e, args) {
                var queryVal = getCoveoQueryValue();
                if( lastQuery != queryVal ){
                    var q = "#q=" + queryVal;

                    if( window.location.search.indexOf("category") >= 0 ){
                        queryCancelled = args.cancel = true;
                        $('body').addClass('loading');
                        var pgSize = SearchStaticFunctions.getParameterByName("pagesize");
                        if( pgSize ){
                            window.location = "/search?pagesize="+pgSize+"#q=" + queryVal;  
                        }
                        else {
                            window.location = "/search#q=" + queryVal;                      
                            return false;
                        }
                    }
                    else {
                        $('body').addClass('loading');
                        Coveo.$('.CoveoFacet').coveo('reset');
                        Coveo.$('#search').coveo('state', 'refineby', '');
                        Coveo.$('.customFacet.src .coveo-selected').each(function(){
                            Coveo.$(this).removeClass('coveo-selected');
                        });
                    }
                }

I can see that the queryCancelled and args.cancel are both being set to true, and my code is hitting the window.location line to redirect. However, the page is stuck in a loading state. Note, this only happens when our custom facet is selected, and when we are on any page other than the first.

Is there any reason the page would be stuck in loading? None of the events after the newQuery appear to be firing, and there are no errors in the console.

Gravatar for flguillemette@coveo.com

Comment by François Lachance-Guillemette, Mar 6, 2017 2:30 PM

If pgSize is defined, you have the following url: /search?pagesize=1#q=query. Shouldn't it be /search#pagesize=1&q=query ?

Also, you should probably be setting window.location.href instead of window.location.

What happens if you remove the redirection? Maybe the errors don't appear in the console because it tries to redirect.

What is executed when going line by line after any window.location call?

Gravatar for hernandez@rdacorp.com

Comment by benaldo, Mar 6, 2017 3:29 PM

Thanks for your help here. I tried a few things to get it to work but in the end decided to refactor the code. Now instead of redirecting on a new query, i clear the existing facets (and added code to clear our custom facet), and then let the query run as it should (rather than cancelling it). I'm still a little curious as to why setting the location wouldn't work, but I find this solution cleaner anyway.

Gravatar for flguillemette@coveo.com

Comment by François Lachance-Guillemette, Mar 6, 2017 3:45 PM

This indeed looks much, much cleaner :)

Basically, what was your use case? Why would you want to clear all facets when sending a new query?

Gravatar for hernandez@rdacorp.com

Comment by benaldo, Mar 6, 2017 3:48 PM

We're using a custom hierarchical facet. That was designed to track it's state using a query string parameter. However, our client find that searching within facets is too confusing for the end user, so they requested that any changes to the search text should clear out all facets. I assume the redirect was seen as the easiest way to accomplish that, but it doesn't look like it was ever working as it should have been.

0 Reply
Ask a question