---
title: "tinymce.util.EventDispatcher"
description: "This class lets you add/remove and dispatch events by name on the specified scope. This makes it easy to add event listener logic to any class."
canonical_url: "https://www.tiny.cloud/docs/tinymce/latest/apis/tinymce.util.eventdispatcher/"
md_url: "https://www.tiny.cloud/docs/tinymce/6/apis/tinymce.util.eventdispatcher/index.md"
version: "6"
last_updated: "2024-04-03T06:12:53Z"
tokens: 1113
---
Important This page documents TinyMCE 6. For new projects, Tiny recommends using the latest, most up-to-date version. [See TinyMCE 8 documentation](../../../latest/apis/tinymce.util.eventdispatcher/).
# tinymce.util.EventDispatcher

This class lets you add/remove and dispatch events by name on the specified scope. This makes it easy to add event listener logic to any class.

## Examples

```javascript
const eventDispatcher = new EventDispatcher();

eventDispatcher.on('click', () => console.log('data'));
eventDispatcher.dispatch('click', { data: 123 });
```

## Summary

### Methods

| Name | Summary | Defined by |
| --- | --- | --- |
| [dispatch()](#dispatch) | Dispatches the specified event by name. | `EventDispatcher` |
| [fire()](#fire) | Fires the specified event by name. <br> *Deprecated in TinyMCE 6.0 and has been marked for removal in TinyMCE 7.0. Use `dispatch` instead.* | `EventDispatcher` |
| [has()](#has) | Returns true/false if the dispatcher has a event of the specified name. | `EventDispatcher` |
| [isNative()](#isNative) | Returns true/false if the specified event name is a native browser event or not. | `EventDispatcher` |
| [off()](#off) | Unbinds an event listener to a specific event by name. | `EventDispatcher` |
| [on()](#on) | Binds an event listener to a specific event by name. | `EventDispatcher` |
| [once()](#once) | Binds an event listener to a specific event by name and automatically unbind the event once the callback fires. | `EventDispatcher` |

## Methods

### dispatch()

```javascript
dispatch(name: String, args: Object?): Object
```
Dispatches the specified event by name.

#### Examples

```javascript
instance.dispatch('event', {...});
```

#### Parameters

- `name (String)` - Name of the event to dispatch
- `args (Object?)` - Event arguments.

#### Return value

- `Object` - Event args instance passed in.

### fire()

```javascript
fire(name: String, args: Object?): Object
```
Fires the specified event by name.

*Deprecated in TinyMCE 6.0 and has been marked for removal in TinyMCE 7.0. Use `dispatch` instead.*

#### Examples

```javascript
instance.fire('event', {...});
```

#### Parameters

- `name (String)` - Name of the event to fire.
- `args (Object?)` - Event arguments.

#### Return value

- `Object` - Event args instance passed in.

### has()

```javascript
has(name: String): Boolean
```
Returns true/false if the dispatcher has a event of the specified name.

#### Parameters

- `name (String)` - Name of the event to check for.

#### Return value

- `Boolean` - true/false if the event exists or not.

### isNative()

```javascript
isNative(name: String): Boolean
```
Returns true/false if the specified event name is a native browser event or not.

#### Parameters

- `name (String)` - Name to check if it’s native.

#### Return value

- `Boolean` - true/false if the event is native or not.

### off()

```javascript
off(name: String?, callback: Function?): Object
```
Unbinds an event listener to a specific event by name.

#### Examples

```javascript
// Unbind specific callback
instance.off('event', handler);

// Unbind all listeners by name
instance.off('event');

// Unbind all events
instance.off();
```

#### Parameters

- `name (String?)` - Name of the event to unbind.
- `callback (Function?)` - Callback to unbind.

#### Return value

- `Object` - Current class instance.

### on()

```javascript
on(name: String, callback: Function, prepend: Boolean): Object
```
Binds an event listener to a specific event by name.

#### Examples

```javascript
instance.on('event', (e) => {
  // Callback logic
});
```

#### Parameters

- `name (String)` - Event name or space separated list of events to bind.
- `callback (Function)` - Callback to be executed when the event occurs.
- `prepend (Boolean)` - Optional flag if the event should be prepended. Use this with care.

#### Return value

- `Object` - Current class instance.

### once()

```javascript
once(name: String, callback: Function, prepend: Boolean): Object
```
Binds an event listener to a specific event by name
and automatically unbind the event once the callback fires.

#### Examples

```javascript
instance.once('event', (e) => {
  // Callback logic
});
```

#### Parameters

- `name (String)` - Event name or space separated list of events to bind.
- `callback (Function)` - Callback to be executed when the event occurs.
- `prepend (Boolean)` - Optional flag if the event should be prepended. Use this with care.

#### Return value

- `Object` - Current class instance.
