onlooker

1.0.4 • Public • Published

onlooker

add observer functionality to any object

var ObserverMixin = require('onlooker');

var Person = function(name) {
	this.name = name;
	this.on('nameChange', this.shoutName);
	return this;
}

ObserverMixin(Person.prototype);

Person.prototype.shoutName = function(newName, oldName) {
	console.log('MY NAME IS', newName.toUpperCase());
};

Person.prototype.changeName = function(name) {
	var oldName = this.name;
	this.name = name;
	this.trigger('nameChange', this.name, oldName);
};
var tintin = new Person('Tintin');

tintin.changeName('Snowy');

// logs: MY NAME IS SNOWY

adds on, off, and trigger methods to the specified object.

supports space delimited event names - e.g:

obj.trigger('created updated', args);

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.0.40latest

Version History

VersionDownloads (Last 7 Days)Published
1.0.40
1.0.21
1.0.10
1.0.00

Package Sidebar

Install

npm i onlooker

Weekly Downloads

1

Version

1.0.4

License

ISC

Last publish

Collaborators

  • ergusto