Simple pubsub service for angularJS
- angular 1.7.3
Download the latest version
or install it with Bower:
bower install angular-pubsub
Then include it:
<script src='angular-pubsub.js'></script>
When using Require.JS:
define(['angular-pubsub'], function(angularPubsub) {
// Write good code here
});
angular.module('myApp',['angularPubsub']);
Then where you need it :
angular.module('myApp').controller('myAppCtrl',['PubSub', function (Pubsub) {
// Do awesome things with
}]);
angular-pubsub provides 3 methods :
subscribe()
publish()
unsubscribe()
The client (controller in most cases) which needs to receive messages has to subscribe to a specific topic:
PubSub.subscribe(topic, callback);
topic
-{string}
- Name of the topic/event (BUS name)callback
-{function}
- Function to call when receiving a message
PubSub.publish(topic, args);
args
-{*}
- Optional one or more arguments which will be passed onto the subscribed clients
When the client does not need to receive messages anymore, and to avoid memory leaks, it is recommended to unsubscribe.
PubSub.unsubscribe(topic, callbackToUnregister);
callbackToUnregister
-{function}
- Registeredcallback
Tests are based on Karma-runner and implemented with Jasmine 2.
angular-pubsub is tested on stable versions of Chrome and Firefox.
Feedback and contributions are welcomed.
npm install
bower install
then
scripts/unit-tests.sh
or
make test
make build
Library is built into dist/