Home

# Class goog.events.EventTarget

Inherit from this class to give your object the ability to dispatch events. Note that this class provides event sending behaviour, not event receiving behaviour: your object will be able to broadcast events, and other objects will be able to listen for those events using goog.events.listen().

The name "EventTarget" reflects the fact that this class implements the EventTarget interface as defined by W3C DOM 2/3, with a few differences:

- Event objects do not have to implement the Event interface. An object is treated as an event object if it has a 'type' property.
- You can use a plain string instead of an event object; an event-like object will be created with the 'type' set to the string value.

Unless propagation is stopped, an event dispatched by an EventTarget will bubble to the parent returned by `getParentEventTarget`. To set the parent, call `setParentEventTarget` or override `getParentEventTarget` in a subclass. Subclasses that don't support changing the parent should override the setter to throw an error.

Example usage:

    var source = new goog.events.EventTarget();
    function handleEvent(event) {
    alert('Type: ' + e.type + '\nTarget: ' + e.target);
    }
    goog.events.listen(source, 'foo', handleEvent);
    ...
    source.dispatchEvent({type: 'foo'}); // will call handleEvent
    // or source.dispatchEvent('foo');
    ...
    goog.events.unlisten(source, 'foo', handleEvent);

    // You can also use the Listener interface:
    var listener = {
    handleEvent: function(event) {
    ...
    }
    };
    goog.events.listen(source, 'bar', listener);

extends goog.Disposable

Instance Method Summary

`addEventListener(string type, ?Object handler, boolean= opt_capture, ?Object= opt_handlerScope)`

Adds an event listener to the event target. The same handler can only be added once per the type. Even if you add the same handler multiple times using the same type then it will only be called once when the event is dispatched. Supported for legacy but use goog.events.listen(src, type, handler) instead.

`dispatchEvent((Object|goog.events.Event|null|string) e) ⇒ boolean`

Dispatches an event (or event like object) and calls all listeners listening for events of this type. The type of the event is decided by the type property on the event object. If any of the listeners returns false OR calls preventDefault then this function will return false. If one of the capture listeners calls stopPropagation, then the bubble listeners won't fire.

`disposeInternal()`

Unattach listeners from this object. Classes that extend EventTarget may need to override this method in order to remove references to DOM Elements and additional listeners, it should be something like this:

    MyClass.prototype.disposeInternal = function() {
    MyClass.superClass_.disposeInternal.call(this);
    // Dispose logic for MyClass
    };

| `getParentEventTarget() ⇒ ?goog.events.EventTarget` Returns the parent of this event target to use for bubbling.
| `removeEventListener(string type, ?Object handler, boolean= opt_capture, ?Object= opt_handlerScope)` Removes an event listener from the event target. The handler must be the same object as the one added. If the handler has not been added then nothing is done.
| `setParentEventTarget(?goog.events.EventTarget parent)` Sets the parent of this event target to use for bubbling.