Class: EventTarget

evento. EventTarget

new EventTarget()

A constructor function for creating event target objects.

var et = new evento.EventTarget();

The methods of an event target object are inspired by the DOM2 standard.

Source:
  • maria.js, line 28

Members

<static> superConstructor

Properties:
Name Type Description
evento.EventTarget.superConstructor
Source:
  • maria.js, line 35

Methods

<static> mixin(obj)

Mixes in the event target methods into any object.

Example 1

app.Person = function(name) {
    evento.EventTarget.call(this);
    this.setName(name);
};
evento.EventTarget.mixin(app.Person.prototype);
app.Person.prototype.setName = function(newName) {
    var oldName = this.name;
    this.name = newName;
    this.dispatchEvent({
        type: "change",
        oldName: oldName,
        newName: newName
    });
};

var person = new app.Person('David');
person.addEventListener('change', function(evt) {
    alert('"' + evt.oldName + '" is now called "' + evt.newName + '".');
});
person.setName('Dave');

Example 2

var o = {};
evento.EventTarget.mixin(o);
o.addEventListener('change', function(){alert('change');});
o.dispatchEvent({type:'change'});
Parameters:
Name Type Description
obj Object

The object to be made into an event target.

Source:
  • maria.js, line 269

addEventListener(type, listener)

If the listener is an object then when a matching event type is dispatched on the event target, the listener object's handleEvent method will be called.

If the listener is a function then when a matching event type is dispatched on the event target, the listener function is called with event target object set as the this object.

One listener (or type/listener pair to be more precise) can be added only once.

et.addEventListener('change', {handleEvent:function(){}});
et.addEventListener('change', function(){});
Parameters:
Name Type Description
type string

The name of the event.

listener Object | function

The listener object or callback function.

Source:
  • maria.js, line 72

addParentEventTarget(parent)

When an event is dispatched on an event target, if that event target has parents then the event is also dispatched on the parents as long as bubbling has not been canceled on the event.

One parent can be added only once.

var o = new evento.EventTarget();
et.addParentEventTarget(o);
Parameters:
Name Type Description
parent EventTarget

A parent to call when bubbling an event.

Source:
  • maria.js, line 128

dispatchEvent(event)

The event.type property is required. All listeners registered for this event type are called with event passed as an argument to the listeners.

If not set, the event.target property will be set to be this event target.

The evt.currentTarget will be set to be this event target.

Call evt.stopPropagation() to stop bubbling to parents.

et.dispatchEvent({type:'change'});
et.dispatchEvent({type:'change', extraData:'abc'});
Parameters:
Name Type Description
event Object

The event object to dispatch to all listeners.

Source:
  • maria.js, line 184

removeEventListener(type, listener)

Removes added listener matching the type/listener combination exactly. If this combination is not found there are no errors.

var o = {handleEvent:function(){}};
et.removeEventListener('change', o);
et.removeEventListener('change', fn);
Parameters:
Name Type Description
type string

The name of the event.

listener Object | function

The listener object or callback function.

Source:
  • maria.js, line 99

removeParentEventTarget(parent)

Removes parent added with addParentEventTarget. If the parent is not found, there are no errors.

var o = {handleEvent:function(){}};
et.removeParentEventTarget(o);
Parameters:
Name Type Description
parent EventTarget

The parent to remove.

Source:
  • maria.js, line 154