0.1.1 • Public • Published

Eventus Build Status

Eventful object that can everything. It's a simple and rapid realization of Pub-Sub, Mediator & Observer patterns.

How to use it


You can create instance of Eventus using its constructor.

var Eventus = require('eventus');
var foo = new Eventus();

Also you can pass options object that will extend simple Eventus object and after taht you can use it as object properties (methods). After creating init event will be triggered.

var bar = new Eventus({
	baz: 'qux'

Event listening

Each Eventus object has amount of built-in events and may has own custom events. Events can be defined on options and by using .on() and .once() methods as well.

var foo = new Eventus({
    events: {
        bar: console.log

foo.trigger('bar', 'Hello World!');
// output in console: Hello World!

foo.on('baz', console.log);
foo.trigget('baz', 'Hello again');
// output in console: Hello again

For one-time listener you can use .once() instead of .on().

To remove listener use .off() method. It can accept different set of arguments.

var foo = new Eventus();
foo.on('bar', console.log);

// remove specified listener
foo.off('bar', console.log);

// remove all listener for specified event

// remove all listeners for all event

Event triggering

For triggering event of object just use .trigger() method. Method accepts at least 1 parameter - event name, all other parameters is data that will be passed for listeners;

var foo = new Eventus();
foo.on('bar', console.log);
foo.trigger('bar', 'Hello', 'World');
// output in console: Hello World

Child elements

Each Eventus object can has child elements. For creating and removing children use .createChild() and .removeChild() methods. .addChild() accepts two parameters: name (alias for child element in parent object) and options (will be passed as options of child element to it's constructor). After adding child element event child:add will be triggered on parent object.

var foo = new Eventus();
foo.createChild('baz', { some: 'option' });

// each child element is available by alias in parent element
// output in console: option

Each child element can has it's own deep child element


For deleting child element you can use both name of object and it's instance. After deleting child element event destroy will be triggered on removed object and child:remove will be triggered on parent object.

var foo = new Eventus();
var bar = foo.createChild('bar');
var baz = foo.createChild('baz');

// remove element using name

// remove element using instance

Destroying object

You can simply destroy object by using .destroy() method. This will remove all listeners from object and triggers destroy event.

Broadcasting & emitting events

For passing events to parent elements use .emit() method, for children - .broadcast() method. Arguments are the same as for triggering events.

var foo = new Eventus();
var bar = foo.createChild('bar');
var baz = bar.createChild('baz');

// this will trigger 'qux' event on bar & baz
foo.broadcast('qux', { some: 'options' });

// this will trigger 'bat' event on foo & bar
baz.emit('bat', { some: 'options'});

List of Eventus modules

Full list of modules


Eventus is published under the MIT License.

Package Sidebar


DownloadsWeekly Downloads






Last publish


  • makzimko