<commit_before># Marionette.Toolkit.StateMixin

`StateMixin` is a public mixin for [`App`](../app.md) or any Marionette class definition that uses a `Backbone.Model` for keeping state.

Use a `StateMixin` if your object/view needs to maintain information that isn't business data.  This provides a consistent method for storing and getting state, along with triggering related events.

## Documentation Index

* [Using StateMixin](#using-statemixin)
* [Setting default state](#setting-default-state)
* [StateMixin's `StateModel`](#statemixins-statemodel)
* [StateMixin's `stateEvents`](#statemixins-stateevents)
* [StateMixin API](#statemixin-api)
  * [Setting State `setState`](#setting-state)
  * [Resetting State `resetStateDefaults`](#resetting-state-defaults)
  * [Getting State `getState`](#getting-state)

### Using StateMixin

While `StateMixin` comes pre-mixined with `Marionette.Toolkit.App` you can extend your own class with `StateMixin` by calling `initState` in your class's `initialize` passing any desired options.

```js
var MyStateModel = Backbone.Model.extend({});

var myClass = Marionette.Object.extend({
  StateModel: MyStateModel

  initialize(options) {
    this.initState(options);
  }
});

_.extend(myClass.prototype, Marrionette.Toolkit.StateMixin)
```

You can also use `Marionette.Toolkit.MixinState` which is a utility to mixin the `StateMixin` into any `Marionette.Object`s or `Marionette.View`s. If there is no `StateModel` definition on your class then the `StateModel` will be defined as a vanilla `Backbone.Model`. However, if you have already defined `StateModel` on your class, your `StateModel` definition **will not be overwritten**.

```js
var MyStateModel = Backbone.Model.extend({});

var myClass = Marionette.Object.extend({
  StateModel: MyStateModel

  initialize(options) {
    this.initState(options);
  }
});

Marionette.Toolkit.MixinState(MyClass);
```

### Setting default state

Because the `StateModel` of the `StateMixin` has to be a `Backbone.Model`, it has access to model `defaults`. `defaults` should be defined on the `StateModel` definition.

```js
var MyToolKitApp = Marionette.Toolkit.App.extend({
  StateModel: {
    defaults: {
      fooState: 'bar'
    }
  }
});

var myToolkitApp = new MyToolKitApp();

myToolkitApp.getState('fooState') === 'bar';
```

### StateMixin's `StateModel`

Define a `StateModel` on your class definition or pass as an option when calling `initState(options)`. This must be
a `Backbone.Model` object definition, not an instance.  If you do not
specify a `StateModel`, a vanilla `Backbone.Model` definition will be used.

#### Declared on Class
```js
var MyStateModel = Backbone.Model.extend({});

var MyClass = Marionette.Toolkit.App.extend({
  StateModel: MyStateModel

  initialize(options) {
    this.initState(options);
  }
});
```

You can also define `StateModel` as a function. In this form, the value
returned by this method is the `StateModel` class that will be instantiated.
When defined as a function, it will receive the `options` passed to the `constructor`.

```js
var MyStateModel = Backbone.Model.extend({});

Marionette.Toolkit.App.extend({
  StateModel: function(options){
    if(options.foo){
      return MyStateModel;<commit_msg>Update docs<commit_after># Marionette.Toolkit.StateMixin

`StateMixin` is a public mixin for [`App`](../app.md) or any Marionette class definition that uses a `Backbone.Model` for keeping state.

Use a `StateMixin` if your object/view needs to maintain information that isn't business data.  This provides a consistent method for storing and getting state, along with triggering related events.

## Documentation Index

* [Using StateMixin](#using-statemixin)
* [Setting default state](#setting-default-state)
* [StateMixin's `StateModel`](#statemixins-statemodel)
* [StateMixin's `stateEvents`](#statemixins-stateevents)
* [StateMixin API](#statemixin-api)
  * [Setting State `setState`](#setting-state)
  * [Resetting State `resetStateDefaults`](#resetting-state-defaults)
  * [Getting State `getState`](#getting-state)

### Using StateMixin

While `StateMixin` comes pre-mixined with `Marionette.Toolkit.App` you can extend your own class with `StateMixin` by calling `initState` in your class's `initialize` passing any desired options.

```js
var MyStateModel = Backbone.Model.extend({});

var myClass = Marionette.Object.extend({
  StateModel: MyStateModel

  initialize(options) {
    this.initState(options);
  }
});

_.extend(myClass.prototype, Marrionette.Toolkit.StateMixin)
```

You can also use `Marionette.Toolkit.MixinState` which is a utility to mixin the `StateMixin` into any `Marionette.Object`s or `Marionette.View`s. If there is no `StateModel` definition on your class then the `StateModel` will be defined as a vanilla `Backbone.Model`. However, if you have already defined `StateModel` on your class, your `StateModel` definition **will not be overwritten**.

```js
var MyStateModel = Backbone.Model.extend({});

var myClass = Marionette.Object.extend({
  StateModel: MyStateModel

  initialize(options) {
    this.initState(options);
  }
});

Marionette.Toolkit.MixinState(MyClass);
```

### Setting default state

Because the `StateModel` of the `StateMixin` has to be a `Backbone.Model`, it has access to model `defaults`. `defaults` should be defined on the `StateModel` definition.

```js
var MyToolKitApp = Marionette.Toolkit.App.extend({
  StateModel: {
    defaults: {
      fooState: 'bar'
    }
  }
});

var myToolkitApp = new MyToolKitApp();

myToolkitApp.getState('fooState') === 'bar';
```

### StateMixin's `StateModel`

Define a `StateModel` on your class definition or pass as an option when calling `initState(options)`. This must be
a `Backbone.Model` object definition, not an instance.  If you do not
specify a `StateModel`, a vanilla `Backbone.Model` definition will be used.

#### Declared on Class
```js
var MyStateModel = Backbone.Model.extend({});

var MyClass = Marionette.Toolkit.App.extend({
  StateModel: MyStateModel

  initialize(options) {
    this.initState(options);
  }
});
```

#### Passed as Option on Initialization
```js
var MyStateModel = Backbone.Model.extend({});

var MyClass = Marionette.Toolkit.App.extend({
  initialize(options) {
    this.initState(options);
  }
});

var myClass = new MyClass({
  StateModel: MyStateModel
});
```

You can also define `StateModel` as a function. In this form, the value
returned by this method is the `StateModel` class that will be instantiated.
When defined as a function, it will receive the `options` passed to the `constructor`.

```js
var MyStateModel = Backbone.Model.extend({});

Marionette.Toolkit.App.extend({
  StateModel: function(options){
    if(options.foo){
      return MyStateModel;
{'fraction_non_alphanumeric': 0.11946050096339114, 'fraction_numerical': 0.0, 'mean_word_length': 5.0142602495543676, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5354657', 'n_tokens_mistral': 2120, 'n_tokens_neox': 2034, 'n_words': 668}