1.0.0 • Published 10 years ago

do.js v1.0.0

Weekly downloads
4
License
MIT
Repository
github
Last release
10 years ago

do.js

Minimalistic Event Dispatcher

About

do.js is a simple event dispatching library written in < 20 lines of code.

See usage on how to use it.

function Do(parent) {
	var listeners = [];
	this.do = function(callback) {
		listeners.push(callback);
	};
	this.undo = function(callback) {
		listeners.splice(listeners.indexOf(callback), 1);
	};
	this.fire = function() {
		for (var v = 0; v < listeners.length; v++) {
			listeners[v].apply(parent, arguments);
		}
	};
}

Why Do?

Not that I want to create yet another xyzsource library/micro-framework, but here's the story.

While on the train to work, I was tinkling with source and needed a simple event listener system.

That's how I wrote this (in probably less than 5 minutes) just because I needed something simple and have something I could understand easily. The api also attempts to be english like while minimise typing.

Do.js can be pronounced as doh-dot-j-s, or do-dot-j-s. Doh like in play dough, do-ra-me, doraemon, domo. Do like in "do this", "I do", or a do-while loop. (My original name for this class would be "ondo", but nvm it..)

One difference between your usual way you handle

element.addEventListener('bla', function(something) {});
vs
element.onBla.on(function(something) {});

Of course, this library may not be of production quality or fitting your usual/preferred event dispatching style, in that case feel free to try out eventdispatcher.js or many other libraries out there.

This event handling approach is however nothing new, and my idea's inception probably started with Robert Penner's (the guy who also brought us easing/tweening equations) related work with Signals in as3. You may also like to check out this page which includes a good comparison of different observer pattern implementations.

Usage

function Cat() {
	this.name = 'cat';
	this.onSay = new Do(this); // Create an event listener type
}

Cat.prototype.says = function(something) {
	console.log('Cat says ' + something);
	this.onSay.fire(something); // Notifies listeners of something
}

var cat = new Cat();

cat.onSay.do(function(something) {  // Adds listener
	console.log('I hear ' + this.name + ' say ' + something);
});

cat.says('Meow!');

Node

Do supports node.js.

var Do = require('./do.js');

var onFire = new Do();
onFire.do(something)

onFire.fire('bla');

Tests

Not yet. You make a pull request.

Contact

For improvements, suggestions, you may use github issues. You may also subscribe to my Twitter: @blurspline

License

MIT