Skip to content
This repository has been archived by the owner on Feb 20, 2019. It is now read-only.

EventGroup

dzearing edited this page Oct 13, 2014 · 2 revisions

Summary

The EventGroup class represents a logical grouping of events registered and owned by an object. Groups of events can be registered with it, and deactivated all at once. This is particularly useful when segregating events to be listened at different stages of an object's lifetime. The EventGroup instance is tied to a provided parent, and automatically assumes all callbacks are tied to that parent, as a convenience to the caller.

Initialization

An EventGroup is typically created at construction. Note that the 'this' pointer is passed into the constructor so that callbacks can be auto binded to it:

	import EventGroup = require('../onejs/EventGroup');
	
	class Foo {
		events = new EventGroup(this); 
	}
	
	export = Foo;

Listening to events

Event callbacks are registered using the "on" method. Events can be either DOM events on elements, or they can be custom events on objects. Example:

this.events.on(window, 'resize', this._onResize);

Events can be very easily disconnected with the "off" method, either specifically:

this.events.off(window, 'resize', this._onResize);

… or less specifically:

this.events.off(window);

… or completely (especially useful at deactivation time):

this.events.off();

Raising events

You can raise custom events from the parent provided at construction. Example:

this.events.raise('change', { property: 'foo', old: '1', new: '2' }, true);

The 3rd parameter is "shouldBubble", which allows for event bubbling if the parent object which the event is firing on has a "parent" property. If callbacks that are fired return 'false', bubbling will be canceled.