simple-event-handler
v1.0.0
Published
Event handler library for both: frontend and backend
Downloads
4
Maintainers
Readme
simple-event-handler
Event handler library for both: front-end and back-end
Table of Contents
Installation
Include File
<script type="text/javascript" src=".../js/simple-event-handler.min.js"></script>
Add simple-event-handler in your angular app to your module.
angular.module('app', ['simple-event-handler']);
Bower
Install via Bower
bower install --save simple-event-handler
npm
Install via npm
npm install --save simple-event-handler
Examples
AngularJS Example
app.run.js
angular
.module('app', ['simple-event-handler'])
.run(['$eventHandler', function ($eventHandler) {
$eventHandler.subscribe('my-event', function(data) {
// your logic here
})
}]);
app.controller.js
angular
.module('app')
.controller('MainController', ['$eventHandler', function ($eventHandler) {
var args = {name: 'some name'}
$eventHandler.fire('my-event', args);
}]);
Node.js Example
var eventHandler = require('simple-event-handler');
eventHandler.subscribe('my-event', function(data) {
// your logic here
})
...
var args = {name: 'some name'}
eventHandler.fire('my-event', args);
Basic Example
var eventHandler = new EventHandler();
eventHandler.subscribe('my-event', function(data) {
// your logic here
})
...
var args = {name: 'some name'}
eventHandler.fire('my-event', args);
Method Chaining
It's just a nice feature to have ability to create a chains like this:
var result = 0;
var handler = function (data) {
result = result + data;
};
eventHandler
.once(EVENT_NAME, function () {
result++;
})
.fire(EVENT_NAME) // result = 1
.fire(EVENT_NAME) // result = 1
.subscribe(EVENT_NAME, handler)
.emit(EVENT_NAME, 10) // result = 11
.on(EVENT_NAME, function (data) {
result = result * 2 - data;
})
.fire(EVENT_NAME, 5) // result = ((11 + 5) * 2) - 5 = 27
.offAll(EVENT_NAME) // removes everything
.emit(EVENT_NAME) // no handlers to execute
.fire(EVENT_NAME) // no handlers to execute
.on(EVENT_NAME_2, handler)
.fire(EVENT_NAME_2, 3) // result = 27 + 3 = 30
.unsubscribe(EVENT_NAME_2, handler)
.fire(EVENT_NAME_2, 20); // no handlers to execute
console.log(result); // --> 30
Documentation
subscribe
Registers new handler function to subscription list for the event named eventName
.
The handler will be added to the end of the subscription list.
Params
| Name | Type | Description |
|:-----|:----:|:-----|
| eventName | String|Array<String>
| String or array of strings with event name(s) |
| handler | Function
| Function with your logic which will be called once event(s) is fired |
| $scope | Object
| AngularJS $scope object |
Handler function has one input parameter with data object which can be passed via #fire(eventName, handler) method
Example
Basic subscription
eventHandler.subscribe('my-event', function(data) {
// your logic here
})
Multi-subscription
var events = ['ev1', 'ev2', 'custom-event', 'service:update'];
eventHandler.subscribe(events, function(data) {
// your logic here
})
fire
Synchronously calls each of the handlers registered for the event named eventName
,
in the order they were registered, passing the supplied arguments to each,
empty object will be passed instead if nothing provided
| Name | Type | Description |
|:-----|:----:|:-----|
| eventName | String
| Event name to fire |
| args | *
| args that will be passed to each handler function. Can be used as shared resource if it is not a primitive value. Default: {}
|
Example
eventHandler.subscribe('report:update', function(data) {
// your logic here
console.log(data.id); // --> 44
})
var report = {
id: 44,
title: 'Cost Report',
isPrivate: false,
storeInDB: false
};
eventHandler.fire('report:update', report);
unsubscribe
Removes registered handler function from subscription list
| Name | Type | Description |
|:-----|:----:|:-----|
| eventName | String
| Event name on which handler was subscribed |
| handler | Function
| Handler function which should be removed from subscription list |
Example
var handler = function(data) {
// your logic here
}
eventHandler.subscribe('my-event', handler);
eventHandler.fire('my-event');
eventHandler.unsubscribe('my-event', handler)
unsubscribeAll
Removes all the registered handler functions for the event named eventName
| Name | Type | Description |
|:-----|:----:|:-----|
| eventName | String
| Event name for which subscription list should be cleaned |
Example
eventHandler.subscribe('my-event', function() {
// handler #1
});
eventHandler.subscribe('my-event', function() {
// handler #2
});
eventHandler.subscribe('my-event', function() {
// handler #3
});
eventHandler.fire('my-event');
eventHandler.unsubscribeAll('my-event')
once
Adds a one time handler function for the event named eventName
.
Handler will be removed from subscription list after first execution
Params
| Name | Type | Description |
|:-----|:----:|:-----|
| eventName | String|Array<String>
| String or array of strings with event name(s) |
| handler | Function
| Function with your logic which will be called once event(s) is fired |
| $scope | Object
| AngularJS $scope object |
Handler function has one input parameter with data object which can be passed via #fire(eventName, handler) method
Example
Basic subscription
eventHandler.once('my-event', function(data) {
// your logic here
})
Multi-subscription
var events = ['ev1', 'ev2', 'custom-event', 'service:update'];
eventHandler.once(events, function(data) {
// your logic here
})
on
Alias for #subscribe(eventName, handler)
emit
Alias for #fire(eventName, args)
off
Alias for #unsubscribe(eventName, handler)
offAll
Alias for #unsubscribeAll(eventName)
License
MIT License
Copyright (c) 2017 Volodymyr Lavrynovych
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.