light-dispatcher

2.0.1 • Public • Published

Handle Application Events

Application wide events may be used in very specific cases where too distant parts of the aplication have to communicate, but it is prefered to use data-binding for communication between components. Light.js uses an internal event pub/sub singleton to manage interactions between differents part of the application:

attach a callback on an event
EventDispatcher.on('eventName', callbackFn, context);
dettach a callback on an event
EventDispatcher.off('eventName', callbackFn, context);
fire an event
//current syntax
EventDispatcher.fire('eventName', [arg1, arg2 arg3]);

//upcomming syntax
EventDispatcher.trigger('eventName', arg1, arg2 arg3);

Request/Handler

EventDispatcher provides a clean way to share data across all your application: request and handler. It also gives a way to first format data before sharing them. You first need to declare a handler by giving a name and a callback that returns the data:

myMethod() {
	var alcoholCommercialText = 'here is our new beverage';
	EventDispatcher.setHandler('alcoholCommercialText', (age)=>{
		return age > 21 ? alcoholCommercialText : 'sorry your are not allowed to see this content';
	});
}

and if you need to retrieve this data, use the request method:

displayCommercial(age) {
    this.props.commmercialText = EventDispatcher.request('alcoholCommercialText', [age]);
}

Note: the request/handler mecanism do not use data-binding so in the above case, if alcoholCommercialText change, the displayCommercial method must be called to update this.props.commmercialText

Readme

Keywords

none

Package Sidebar

Install

npm i light-dispatcher

Weekly Downloads

1

Version

2.0.1

License

MIT

Last publish

Collaborators

  • nicolas-riciotti