Giter Club home page Giter Club logo

events's Introduction

events.js

Stupidly simple events.

I found myself writing this code over and over again, so figured I'd zip it up in a library and save myself the trouble of doing it again. Just about 430 bytes gzipped (don't worry, there aren't any dependencies either), events.js implements a -- stupidly simple, and appropriately tiny -- event interface.

How simple? This simple.

var events = Events();

events.on( 'cupcake', function () {
	alert( 'yum!' );
} );

events.fire( 'cupcake' );

// yum!

Just four simple methods:

  • on()
  • one()
  • off()
  • fire()

They do... what you would expect them to do.

API documentation

Events.prototype.on( eventName, handler )

Parameters:

  • eventName {string}
  • handler {function}

Attaches a handler a given event. When the event is fired, (surprise suprise) the handler will be called.

events.on( 'cookie', function ( cookie ) {
	eat( cookie );
} );

events.on( 'full', function () {
	stopEating();
} );

Events.prototype.one( eventName, handler )

Parameters:

  • eventName {string}
  • handler {function}

Like .on(), but with a twist โ€“ the handler is only called once. If eventName is fired subseqent times, the handler won't be called again.

events.one( 'bloated', function () {
	die();
} );

Events.prototype.off( eventName[, handler] )

Parameters:

  • eventName {string}
  • handler {function} (optional)

This function is used to remove event handler(s). If you would like to remove all event handlers attached to a particular event, use:

events.off( 'cookie' );

If you only want to remove one specific handler:

var handler = function () { alert( 'yay!' ) };
events.on( 'cookie', handler );

[...]

events.off( 'cookie', handler );

Events.prototype.fire( eventName[, arguments] )

Parameters:

  • eventName {string}
  • arguments {...mixed} (optional)

Trying to fire an event? You're in luck! In its simplest form:

events.fire( 'cheese' );

This calls all handlers previously attached to the cheese event using .on() (and .one()) with no arguments. If you want to pass arguments...

events.on( 'cookie', function ( cookie ) {
	eat( cookie );
} );

[...]

var cookie = new Cookie();
events.fire( 'cookie', cookie );

You can pass an unlimited number of arguments โ€“ that's right, knock yourself out!

Bonus feature

You can restrict the names of events available by passing in an array when initializing:

var limitedEvents = Events( [ 'joy', 'celebration' ] );

limitedEvents.on( 'death', function () {
	alert( ':(' );
} );
// Error: The event "death" is not defined

If you later attempt to fire or add a handler to an event that wasn't in the initial array, an error will be thrown.

Get it

Available via npm:

npm install stupidly-simple-events

or just download events.min.js.

events's People

Contributors

theopolisme avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

enterprisey

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.