subscribable.js

0.0.4 • Public • Published

Subscribable

A tiny extendable JS constructor for custom event listening. Use it in the browser or in node:

Install

npm install subscribable.js

OR

bower install subscribable

Simple usage

var Daniel = new Subscribable();

Daniel.on('puke', function(msg) {
	console.log('Eeeww! ' + msg);
});

Daniel.trigger('puke', 'That was gross!');
//=> Eeeww! That was gross!

Add events to any constructor

This example uses the Obj.extend function from object-subscribe to make a Person contructor inherit event subscribing features from Subscribable:

var Person = function(name) {
	// add properties from `new Subscribable()` to `this`:
	Obj.extend(new Subscribable(), this);
	this.name = name;
};

Person.prototype = Obj.extend(Subscribable.prototype, {
	sayName: function() {
		console.log('Hi! I\'m ' + this.name + '.');
	}
});

var Daniel = new Person('Daniel');

Daniel.sayName();
//=> Hi! I'm Daniel.

Daniel.on('smile', function showTeeth(numTeeth) {
	console.log('Oh look! ' + this.name + ' has ' + numTeeth + ' teeth.');
});

Daniel.trigger('smile', 30);
//=> Oh look! Daniel has 30 teeth.

Extend

Add your own methods to the Subscribable prototype:

Subscribable.prototype.once = function(event, fn) {
	var t = this;
	this.on(event, function() {
		fn.apply(this, arguments);
		t.off(event, fn);
	});
};

Package Sidebar

Install

npm i subscribable.js

Weekly Downloads

0

Version

0.0.4

License

MIT

Last publish

Collaborators

  • daniel-hug