Code Monkey home page Code Monkey logo

eventus's Introduction

Eventus Current version Build status

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

How to use it

Constructor

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
foo.off('bar');

// remove all listeners for all event
foo.off();

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('bar');
foo.createChild('baz', { some: 'option' });

// each child element is available by alias in parent element
console.log(foo.baz.some);
// output in console: option

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

foo.bar.createChild('qux');

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
foo.removeChild('bar');

// remove element using instance
foo.removeChild(baz);

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

License

Eventus is published under the MIT License.

eventus's People

Contributors

makzimko avatar

Stargazers

Juliia Chubatiuk avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.