src/includes/suggestions.js
function (query, customParams, requestOptions) {
        var response,
            that = this,
            options = that.options,
            noCallbacks = requestOptions && requestOptions.noCallbacks,
            useEnrichmentCache = requestOptions && requestOptions.useEnrichmentCache,
            method = requestOptions && requestOptions.method || that.requestMode.method,
            params = that.constructRequestParams(query, customParams),
            cacheKey = $.param(params || {}),
            resolver = $.Deferred();

        response = that.cachedResponse[cacheKey];
        if (response && $.isArray(response.suggestions)) {
            resolver.resolve(response.suggestions);
        } else {
            if (that.isBadQuery(query)) {
                resolver.reject();
            } else {
                if (!noCallbacks && options.onSearchStart.call(that.element, params) === false) {
                    resolver.reject();
                } else {
                    that.doGetSuggestions(params, method)
                        .done(function (response) {
                            // if response is correct and current value has not been changed
                            if (that.processResponse(response) && query == that.currentValue) {

                                // Cache results if cache is not disabled:
                                if (!options.noCache) {
                                    if (useEnrichmentCache) {
                                        that.enrichmentCache[query] = response.suggestions[0];
                                    } else {
                                        that.enrichResponse(response, query);
                                        that.cachedResponse[cacheKey] = response;
                                        if (options.preventBadQueries && response.suggestions.length === 0) {
                                            that.badQueries.push(query);
                                        }
                                    }
                                }

                                resolver.resolve(response.suggestions);
                            } else {
                                resolver.reject();
                            }
                            if (!noCallbacks) {
                                options.onSearchComplete.call(that.element, query, response.suggestions);
                            }
                        }).fail(function (jqXHR, textStatus, errorThrown) {
                        resolver.reject();
                        if (!noCallbacks && textStatus !== 'abort') {
                            options.onSearchError.call(that.element, query, jqXHR, textStatus, errorThrown);
                        }
                    });
                }
            }
        }
        return resolver;
    }
function (query, customParams, requestOptions) {
        var response,
            that = this,
            options = that.options,
            noCallbacks = requestOptions && requestOptions.noCallbacks,
            useEnrichmentCache = requestOptions && requestOptions.useEnrichmentCache,
            method = requestOptions && requestOptions.method || that.requestMode.method,
            params = that.constructRequestParams(query, customParams),
            cacheKey = $.param(params || {}),
            resolver = $.Deferred();

        response = that.cachedResponse[cacheKey];
        if (response && $.isArray(response.suggestions)) {
            resolver.resolve(response.suggestions);
        } else {
            if (that.isBadQuery(query)) {
                resolver.reject();
            } else {
                if (!noCallbacks && options.onSearchStart.call(that.element, params) === false) {
                    resolver.reject();
                } else {
                    that.doGetSuggestions(params, method)
                        .done(function (response) {
                            // if response is correct and current value has not been changed
                            if (that.processResponse(response) && query == that.currentValue) {

                                // Cache results if cache is not disabled:
                                if (!options.noCache) {
                                    if (useEnrichmentCache) {
                                        that.enrichmentCache[query] = response.suggestions[0];
                                    } else {
                                        that.enrichResponse(response, query);
                                        that.cachedResponse[cacheKey] = response;
                                        if (options.preventBadQueries && response.suggestions.length === 0) {
                                            that.badQueries.push(query);
                                        }
                                    }
                                }

                                resolver.resolve(response.suggestions);
                            } else {
                                resolver.reject();
                            }
                            if (!noCallbacks) {
                                options.onSearchComplete.call(that.element, query, response.suggestions);
                            }
                        }).fail(function (jqXHR, textStatus, errorThrown) {
                        resolver.reject();
                        if (!noCallbacks && textStatus !== 'abort') {
                            options.onSearchError.call(that.element, query, jqXHR, textStatus, errorThrown);
                        }
                    });
                }
            }
        }
        return resolver;
    }
function ( query , customParams , requestOptions ) { var response , that = this , options = that . options , noCallbacks = requestOptions && requestOptions . noCallbacks , useEnrichmentCache = requestOptions && requestOptions . useEnrichmentCache , method = requestOptions && requestOptions . method || that . requestMode . method , params = that . constructRequestParams ( query , customParams ) , cacheKey = $ . param ( params || { } ) , resolver = $ . Deferred ( ) ; response = that . cachedResponse [ cacheKey ] ; if ( response && $ . isArray ( response . suggestions ) ) { resolver . resolve ( response . suggestions ) ; } else { if ( that . isBadQuery ( query ) ) { resolver . reject ( ) ; } else { if ( ! noCallbacks && options . onSearchStart . call ( that . element , params ) === false ) { resolver . reject ( ) ; } else { that . doGetSuggestions ( params , method ) . done ( function ( response ) { // if response is correct and current value has not been changed if ( that . processResponse ( response ) && query == that . currentValue ) { // Cache results if cache is not disabled: if ( ! options . noCache ) { if ( useEnrichmentCache ) { that . enrichmentCache [ query ] = response . suggestions [ 0 ] ; } else { that . enrichResponse ( response , query ) ; that . cachedResponse [ cacheKey ] = response ; if ( options . preventBadQueries && response . suggestions . length === 0 ) { that . badQueries . push ( query ) ; } } } resolver . resolve ( response . suggestions ) ; } else { resolver . reject ( ) ; } if ( ! noCallbacks ) { options . onSearchComplete . call ( that . element , query , response . suggestions ) ; } } ) . fail ( function ( jqXHR , textStatus , errorThrown ) { resolver . reject ( ) ; if ( ! noCallbacks && textStatus !== 'abort' ) { options . onSearchError . call ( that . element , query , jqXHR , textStatus , errorThrown ) ; } } ) ; } } } return resolver ; }
Get suggestions from cache or from server
@param {String} query
@param {Object} customParams parameters specified here will be passed to request body
@param {Object} requestOptions
@param {Boolean} [requestOptions.noCallbacks]  flag, request competence callbacks will not be invoked
@param {Boolean} [requestOptions.useEnrichmentCache]
@return {$.Deferred} waiter which is to be resolved with suggestions as argument
Get suggestions from cache or from server