Angular service for https://github.com/millermedeiros/js-signals (Custom Event/Messaging system for JavaScript)
[angular.js] (https://angularjs.org/)
js-signals version: ~1.0.0
Get and install the requirements.
- Install with Bower
$ bower install angular-jssignals
- or get the javascript in dist folder: angular-jssignals.js
Add the module to an angular application:
angular.module('yourmodule', ['jsSignalsServiceModule']);
Configure the SignalsServiceProvider
:
angular.module('your-app-module', ['jsSignalsServiceModule'])
.config(config);
/* @ngInject */
function config(SignalsServiceProvider) {
SignalsServiceProvider.config({
signals: {
eventItemAdded: 'itemadded',
eventItemRemoved: 'itemremoved'}
}
});
}
Consume the service:
angular.module('your-app-module', ['jsSignalsServiceModule'])
.factory('ExamplePublishService', ExamplePublishService)
/* config code ommitted */
.controller('ExampleTriggerCtrl', ExampleTriggerCtrl)
.controller('ExampleListenerCtrl', ExampleListenerCtrl);
/* @ngInject */
function ExamplePublishService( SignalsService ) {
//a service that adds things to something...
return {
add: function( value ) {
//... add thing code logic
//broadcast signal
SignalsService.itemadded.emit(value);
}
}
}
/* @ngInject */
function ExampleTriggerCtrl( ExamplePublishService ) {
someHandler = function() {
//some handler that calls the service that adds things
ExamplePublishService.additem('some value');
};
}
/* @ngInject */
function ExampleListenerCtrl( SignalsService ) {
//bound a listener to the event signal
SignalsService.itemadded.listen(function( value ) {
//some handler that wants to listen to signal when things are added
//do something with value
});
}
The service expose these methods that mimic the interface of js-signals Signal object:
register(key)
: register an event signalemit(key, data)
: emit/publish/dispatch the signal to litenerslisten(key, callback, options)
: bound a listener handler to the signalunlisten(key, callback, options)
: remove a listener handler from the emit/publish/dispatch queueunlistenAll(key)
: removes all listener handlers from the emit/publish/dispatch queue (clear queue)isListening(key, callback, options)
: verifies if listener handler is bound to the signalgetNumListeners(key)
: get number of listeners bounded to the signaldispose(key)
: destroys the signalforget(key)
: forget memorized data argumentsget(key)
: get the signal
Also, the service expose a property for each event signal registered:
So, if an event signal named 'itemadded' is registered we can do:
SignalsService.itemadded.<emit|listen|unlisten|unlisten|isListening|getNumListeners|dispose|forget|get>(...);
//examples:
SignalsService.itemadded.listen(function() {
//....
});
SignalsService.itemadded.emit(value);
var signal = SignalsService.itemadded.get();
For more information, consult js-signals documentation and/or the documentation and comments in the code.
- This is a work in progress.
- Contributions and comments are welcome.
- João Carvalho
Copyright (c) 2015 João Carvalho
Licensed under the MIT License