Skip to content

Simple pub-sub library for enhancing regular JS objects with event interface. No jQuery and no DOM

License

Notifications You must be signed in to change notification settings

ninio/eventy.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 

Repository files navigation

eventy.js

Simple pub-sub library for enhancing regular JS objects with event interface. No jQuery and no DOM. To event enable the type you've just created simply use eventy.eventEnable( YourObject );

Let's say you are implementing a car

function Car( type ) {
	this.type = type;
}

To enable it's events just type

eventy.eventEnable( Car );

Next you can trigger any event

Car.prototype.setType = function ( newType ) {
	if( newType !== this.type ) {
		this.type = newType;
		Car.triggerEvent( 'typeChange', { type: newType } );
	}
};

And you can handle any event

Car.addEventListener( 'typeChange', function( options ) {
	console.log( options.type );
	console.log( this );
});

Now you can trigger the event from the instance and handle it from the Type

var car1 = new Car( 'sedan' ),
	car2 = new Car( 'truck' );

car1.setType( 'truck' );
car2.setType( 'cabrio' );

You can also subscribe for a specific instance

car1.addEventListener( 'typeChange', function ( options ) {
	console.log( 'car1 event:' );
	console.log( options.type );
});

About

Simple pub-sub library for enhancing regular JS objects with event interface. No jQuery and no DOM

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published