You can use Chic Event on the server side with Node.js and npm:
$ npm install chic-event
On the client side, you can either install Chic Event through Component:
$ component install rowanmanning/chic-event
or by simply including
chic-event.js in your page:
In Node.js or using Component, you can include Chic Event in your script by using require:
var chicEvent = require'chic-event';var Event = chicEventEvent;var EventEmitter = chicEventEventEmitter;
If you're just including with a
EventEmitter are available in the
var Event = chiceventEvent;var EventEmitter = chiceventEventEmitter;
The rest of the examples assume you've got the
EventEmitter variables already.
Chic Event's classes are build with Chic. Read the documentation to familiarise yourself with the API.
EventEmitter class is best used by extending it. Doing so will give your new classes the ability to handle and emit events.
EventEmitter has an
extend static method; you can create your own class like this:
var Animal = EventEmitterextend;var fluffy = ;
Once you have a class, you'll be able to use the following methods:
Bind a handler to an event type. This accepts two arguments:
type: (string) The type of event to bind to.
handler: (function) What to call when this type of event is emitted.
Emit an event. This accepts two arguments:
type: (string) The type of event to emit.
event: (Event|mixed) The event object to pass to all handlers, or data with which to construct an event object.
The following examples are equivalent:
var event = food: 'steak';fluffyemit'eat' event;
fluffyemit'eat' food: 'steak';
Each of the handlers bound to the given event type will be called with the
Event object as their first argument:
fluffyon'eat'if eventdatafood !== 'steak'return 'Fluffy sicked up his ' + eventdatafood;;fluffyemit'eat' food: 'carrots'; // Fluffy sicked up his carrots
Remove a specific handler from an event type. This accepts two arguments:
type: (string) The type of event to remove the handler from.
handler: (function) The handler to remove.
fluffyon'eat' onEat; // bind a handlerfluffyoff'eat' onEat; // then remove it
Remove all handlers from an event type. This accepts one argument:
type: (string) The type of event to remove the handlers from.
fluffyon'eat' ; // bind handlersfluffyon'eat' ;fluffyoff'eat'; // then remove them
Remove all handlers from all event types. Call with no arguments.
fluffyon'eat' ; // bind handlersfluffyon'poop' ;fluffyoff; // then remove them
Event class is used to hold event data and allow handlers to stop events, preventing further handlers from executing.
This property contains the type of the event. It defaults to
null and is set when passed into
var event = ;fluffyemit'eat' event;eventtype; // eat
var event = ;fluffyemit'eat' event;eventtarget === fluffy; // true
This property contains the arbitrary event data which can be passed in during construction:
var event = food: 'steak';eventdatafood; // steak
EventEmitter.emit is called with anything other than an
Event instance, then a new
Event is created with the data passed into the constructor:
fluffyon'eat'eventdatafood; // steak;fluffyemit'eat' food: 'steak';
Event handlers are executed in the order they are added. This method allows a handler to prevent execution of any other handlers later in the stack:
fluffyon'eat'eventstop;;fluffyon'eat'// never called;fluffyemit'eat';
This method returns whether the
Event instance has been stopped with
var event = ;eventstopped; // falseeventstop;eventstopped; // true
To develop Chic Event, you'll need to clone the repo and install dependencies:
$ npm install
No code will be accepted unless all tests are passing and there are no lint errors. Commands are outlined below:
Run JSHint with the correct config against the code-base:
$ npm run-script lint
Run unit tests on the command line in a Node environment:
$ npm test
Chic Event is licensed under the MIT license.