Event handler library for both: front-end and back-end
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']);
Install via Bower
bower install --save simple-event-handler
Install via npm
npm install --save simple-event-handler
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);
}]);
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);
var eventHandler = new EventHandler();
eventHandler.subscribe('my-event', function(data) {
// your logic here
})
...
var args = {name: 'some name'}
eventHandler.fire('my-event', args);
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
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` |
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
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
})
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: {} |
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);
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 |
var handler = function(data) {
// your logic here
}
eventHandler.subscribe('my-event', handler);
eventHandler.fire('my-event');
eventHandler.unsubscribe('my-event', handler)
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 |
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')
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` |
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
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
})
Alias for #subscribe(eventName, handler)
Alias for #fire(eventName, args)
Alias for #unsubscribe(eventName, handler)
Alias for #unsubscribeAll(eventName)
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.