GithubHelp home page GithubHelp logo

willywongi / biee Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 144 KB

Browser Inspired Event Emitter: events with subscribers, bubbling, default behaviours.

License: Apache License 2.0

JavaScript 100.00%

biee's Introduction

Browser Inspired Event Emitter

Yet Another Event Emitter. This API is heavily inspired by YUI EventTarget, which in turn, is borrowed by the browser: Features default behaviour and bubble targets.

  • on(evtName, callback): callback will be invoked whenever the event evtName fires.
  • once(evtName, callback): callback will be invoked just once, when the event evtName fires.
  • fire(evtName, arg1, arg2, ...): Fires the event evtName. Subsequent arguments will be passed to subscribed callbacks.
  • off(evtName, callback): Removes callback from the subscribers.
  • publish(evtName, {options}): Lets you prepare some options for the given event (see below).
  • addTarget(anotherEventTarget): Adds another event target. Events fired on the first target will "bubble up" to the added event target.

The options for publish are (option: default):

  • defaultFn: null, the function that will be called if the event cycle goes through the end,
  • broadcast: false, whether or not the GlobalTarget is notified when the event is fired,
  • preventable: true, whether or not preventDefault() has an effect

When a callback runs, it gets the event Facade as the first argument and any subsequent argument passed to fire. The Facade object has this methods:

  • preventDefault: Prevents the default behaviour
  • stopPropagation: Stops the propagation of the event to the next bubble targets
  • stopImmediatePropagation: For this particular event, no other listener will be called. Neither those attached on the same element, nor those attached on bubble targets.

Example:

var EventTarget = require('biee').EventTarget,
	t = new EventTarget();

// subscribe, fire an event with a single argument
t.on('greet', function(e, who) {
	console.log('Greetings, ' + who + '!');
});
t.fire('greet', 'Anna');

// publish an event with a default behaviour, listeners can prevent it.
t.publish('wavehands', function(e, who) {
	moveArmTowards(who);
});
t.on('wavehands', function(e, who) {
	if (who == 'someBadGuy') {
		e.preventDefault();
	}
});
t.fire('wavehands', 'someNiceGuy');
t.fire('wavehands', 'someBadGuy');

// add a bubbling target
var t2 = new EventTarget();
t.addTarget(t2);
t2.on('greet', function(e, who) {
	console.log('Cheers from t2 too!');
});
t.fire('greet', 'Bob');

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.