# 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
- debugInfo
- disable
- enable
- get
- getAttributes
- getDefault
- getEventName
- registerNewAttribute
- reset
- set
- setDefault
- setMultiple
- setNewDefault

### Properties

- attributes
- disabled
- logger
- ID

### Object literals

- eventTypes

### Constructors

- constructor

## Methods

### atLeastOneFacetIsActive

- atLeastOneFacetIsActive(): boolean

- - Defined in models/QueryStateModel.ts :125

  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

- Inherited from BaseComponent.debugInfo

  - Defined in ui/Base/BaseComponent.ts :41

  Return the debug info about this component.

  #### Returns any

### disable

- disable(): void

- Inherited from BaseComponent.disable

  - Defined in ui/Base/BaseComponent.ts :52

  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

- Inherited from BaseComponent.enable

  - Defined in ui/Base/BaseComponent.ts :61

  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

- Inherited from Model.get

  - Defined in models/Model.ts :185

  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

- Inherited from Model.getAttributes

  - Defined in models/Model.ts :96

  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

- Inherited from Model.getDefault

  - Defined in models/Model.ts :200

  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

- Inherited from Model.getEventName

  - Defined in models/Model.ts :238

  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

- Inherited from Model.registerNewAttribute

  - Defined in models/Model.ts :225

  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

- Inherited from Model.reset

  - Defined in models/Model.ts :213

  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

- Inherited from Model.set

  - Defined in models/Model.ts :85

  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

- Inherited from Model.setDefault

  - Defined in models/Model.ts :174

  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

- Inherited from Model.setMultiple

  - Defined in models/Model.ts :124

  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

- Inherited from Model.setNewDefault

  - Defined in models/Model.ts :161

  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>

Inherited from Model.attributes

- Defined in models/Model.ts :45

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

Inherited from BaseComponent.disabled

- Defined in ui/Base/BaseComponent.ts :23

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

type

  {boolean}

### logger

logger: Logger

Inherited from BaseComponent.logger

- Defined in ui/Base/BaseComponent.ts :18

Allows component to log in the dev console.

### Static ID

ID: string

Inherited from BaseComponent.ID

- Defined in ui/Base/BaseComponent.ts :28

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

Inherited from Model.eventTypes

- Defined in models/Model.ts :58

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

- new QueryStateModel(element: HTMLElement, attributes?: IStringMap<string>): QueryStateModel

- - Defined in models/QueryStateModel.ts :108

  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

- Model