Class QueryStateModel

The QueryStateModel class is a key-value store which contains the current state of the components that can affect the query (see State). This class inherits from the Model class. Optionally, it is possible to persist the state in the query string in order to enable browser history management (see the HistoryController class).

Components set values in the QueryStateModel instance to reflect their current state. The QueryStateModel triggers state events (see eventTypes) whenever one of its values is modified. Components listen to triggered state events to update themselves when appropriate.

For instance, when a query is triggered, the Searchbox component sets the q attribute (the basic query expression), while the Pager component sets the first attribute (the index of the first result to display in the result list), and so on.

Example:

The user modifies the content of the Searchbox and submits a query. This triggers the following state events:

  • state:change:q (because the value of q has changed).
  • state:change (because at least one value has changed in the QueryStateModel).

Components or external code can attach handlers to those events:

Coveo.$$(document).on('state:change:q', function() {
  [ ... ]
});

Note:

Normally, you should interact with the QueryStateModel instance using the Coveo.state top-level function.

Index

Methods

atLeastOneFacetIsActive

  • atLeastOneFacetIsActive(): boolean
  • Validates whether at least one facet is currently active (has selected or excluded values) in the interface.

    Returns boolean

    true if at least one facet is active; false otherwise.

debugInfo

  • debugInfo(): any

disable

  • disable(): void
  • Disable the component. Normally this means that the component will not execute handlers for the framework events (query events, for example). Components are enabled by default on creation.

    Returns void

enable

  • enable(): void
  • Enable the component. Normally this means that the component will execute handlers for the framework events (query events, for example). Components are enabled by default on creation.

    Returns void

get

  • get(attribute?: string): any
  • Gets the value of a single specific attribute.
    If no attribute is specified, the method instead returns an object containing all registered attribute key-values.

    Parameters

    • Optional attribute: string

      the specific attribute whose value should be returned.

    Returns any

getAttributes

  • getAttributes(): object
  • Gets an object containing all active registered attribute key-values.
    An attribute is considered active when its value is not in its default state.

    Returns object

    • [key: string]: any

getDefault

  • getDefault(attribute?: string): any
  • Gets the default value of a single specific attribute.
    If no attribute is specified, the method instead returns an object containing all registered attribute key-default values.

    Parameters

    • Optional attribute: string

      the specific attribute whose default value should be returned.

    Returns any

getEventName

  • getEventName(event: string): string
  • Gets a string displaying the event namespace followed by the specific event name. The returned string is formatted thus:
    [eventNameSpace]:[eventName]

    example

    getEventName("reset"); could return "state:reset".

    Parameters

    • event: string

      the event name.

    Returns string

registerNewAttribute

  • registerNewAttribute(attribute: string, defaultValue: any): void
  • Registers a new attribute key-value.

    Parameters

    • attribute: string

      the name of the new attribute to register.

    • defaultValue: any

      the newly registered attribute default value.

    Returns void

reset

  • reset(): void
  • Resets each registered attribute to its default value.
    Note: this method calls the setMultiple method without specifying any options.
    After the setMultiple call has returned, this method triggers the reset event.

    Returns void

set

  • set(attribute: string, value: any, options?: IModelSetOptions): void
  • Sets the value of a single specific attribute.
    Note: this method calls the setMultiple method.

    Parameters

    • attribute: string

      the specific attribute whose value is to be set.

    • value: any

      the value to set the attribute to.

    • Optional options: IModelSetOptions

      the options (see setMultiple).

    Returns void

setDefault

  • setDefault(attribute: string): void
  • Sets a single specific attribute to its default value.
    Note: this method calls the setMultiple method without specifying any option.

    Parameters

    • attribute: string

      the specific attribute whose value is to be set to its default value.

    Returns void

setMultiple

  • setMultiple(toSet: object, options?: IModelSetOptions): void
  • Sets the values of one or many attributes.
    This method may trigger the following events (in order):
    preprocess
    changeOne
    change
    all

    Parameters

    • toSet: object

      the key-value list of attributes with their new intended values.

      • [key: string]: any
    • Optional options: IModelSetOptions

      if the customAttribute option is set to true, the method will not validate whether an attribute is registered or not.
      If the validateType option is set to true, the method will ensure that each value type is correct.
      If the silent option is set to true, then the changeOne, change and all events will not be triggered.

    Returns void

setNewDefault

  • setNewDefault(attribute: string, value: any, options?: IModelSetOptions): void
  • Sets a new default value to a single specific attribute.
    Note: specifying a new attribute default value does not set the attribute to that value. This can be done using the setDefault method.

    Parameters

    • attribute: string

      the specific attribute whose default value is to be changed.

    • value: any

      the new intended default value.

    • Optional options: IModelSetOptions

      if the customAttribute option is set to true, the method will not validate whether the attribute is registered or not.

    Returns void

Properties

attributes

attributes: IStringMap<any>

The attributes contained in this model.
Normally, you should not set attributes directly on this property, as this would prevent required events from being triggered.

disabled

disabled: boolean

A disabled component will not participate in the query, or listen to ComponentEvents.

type

{boolean}

logger

logger: Logger

Allows component to log in the dev console.

Static ID

ID: string

The static ID that each component needs in order to be identified.
For example, SearchButton -> static ID: SearchButton -> className: CoveoSearchButton

Object literals

Static eventTypes

eventTypes: object

The event types that can be triggered:
preprocess: triggered before a value is set on an attribute. This allows the value to be modified before it is set.
changeOne: triggered when a single value changes.
change: triggered when one or many values change.
reset: triggered when all attributes are reset to their default values.
all: triggered after the change event.

type

{{preprocess: string, changeOne: string, change: string, reset: string, all: string}}

Constructors

constructor

  • Creates a new QueryStateModel instance.

    Parameters

    • element: HTMLElement

      The HTMLElement on which to instantiate the QueryStateModel.

    • Optional attributes: IStringMap<string>

      The state key-value store to instantiate the QueryStateModel with.

    Returns QueryStateModel

Hierarchy