angular-PubSub
Angular 1.x implementation of the Publish–Subscribe pattern.
NOTE: This repository is no longer maintained.
The current repository and the
npm
package are not going anywhere but for future projects, please consider using PubSub package instead. Check theexamples
folder for a working example in Angular 1.x application.
Installation
Git installation
$ git clone https://github.com/georapbox/angular-PubSub.git
npm installation
$ npm install angular-PubSub --save
Bower installation
$ bower install angular.pubsub
Using PubSub
Register
Include angular-pubsub.js
in your project after angular.js
and register to your application:
var app = angular;
Inject as dependancey
Inject the service as a dependancy of the application modules, to use it:
var MyController = app;
Subscribing events
The "listener" is the function to be executed when an event is emitted.
{ console; console; console;} // Subscribe to eventvar sub = PubSub; // Subscribe to event and execute only one timevar subOnce = PubSub
Publishing events
The publish
method takes two arguments:
- The first one is the name of the event.
- The second one (optional) is the data we may want to pass along as. We can pass data along using an array or an object as shown below.
PubSub;
Unsubscribing events
There are two ways to unsubscribe an event:
- Unsubscribe from a specific topic based on a tokenized reference to the subscription.
PubSub;
- Unsubscribe from a specific topic based on topic name. This way we can unsubscribe all events with the same name.
PubSub;
Methods aliases
on
-subscribe
once
-subscribeOnce
trigger
-publish
off
-unsubscribe
Minify
$ npm run minify