Skip to content

abidibo/js-event-dispatcher

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

js-event-dispatcher

Build Status Coverage Status

A simple js event dispatcher which implements the mediator pattern

Install

NPM

$ npm install --save eventdispatcher

Usage

    import EventDispatcher from 'js-event-dispatcher'

    let mycb = (evtName, param) => console.log(evtName, param)
    EventDispatcher.register('myEvent', mycb)

    EventDispatcher.emit('myEvent', 'lol') // logs 'myEvent', 'lol'

    EventDispatcher.unregister('myEvent', mycb)

For browser usage just add dist/EventDispatcher.js in your document, i.e.

<head>
    <script src="dist/EventDispatcher.js"></script>
</head>

Methods

register

Registers a function to an event

register(evtName, callback, bind?)
Param Type Description
evtName String just the event name
callback Function callback called when the evtName is emitted
bind Object The object to which will point this keyword inside the callback, if empty callback will binded to EventDispatcher object

The callback will be called with the following parameters:

callback(evtName, ...params)

Where ...params may be injected by who emits the event

unregister

Unregisters all functions from an event, or just the given one

unregister(evtName, callback?)
Param Type Description
evtName String the event name
callback Function if given only that callback will be unregistered

emit

Emits an event

emit(evtvName, params?)
Param Type Description
evtName String the event name
params Mixed other parameters that will be passed to the registered callbacks

Testing

Run tests with coverage report

$ npm tun test

Update html report

$ npm run coverage-html

About

A simple event dispatcher written following the mediator pattern

Resources

Stars

Watchers

Forks

Packages

No packages published