https://github.com/vermicida/ng-pubsub
ng-pubsub is an AngularJS module that simplifies the way the siblings scopes -or any two other pieces of code- talk each other.
https://github.com/vermicida/ng-pubsub
Last synced: about 2 months ago
JSON representation
ng-pubsub is an AngularJS module that simplifies the way the siblings scopes -or any two other pieces of code- talk each other.
- Host: GitHub
- URL: https://github.com/vermicida/ng-pubsub
- Owner: vermicida
- License: mit
- Created: 2016-05-07T17:32:06.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2016-05-08T17:56:58.000Z (about 9 years ago)
- Last Synced: 2025-03-22T21:07:05.138Z (about 2 months ago)
- Language: JavaScript
- Homepage:
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ng-pubsub
**ng-pubsub** is an AngularJS module that simplifies the way the siblings scopes -or any two other pieces of code- talk each other. AngularJS gives you _$emit_ to dispatch events from a scope to the top of its hierarchy, and _$broadcast_ to dispatch them to the bottom, but no mechanism is given to do that with siblings scopes. **ng-pubsub** fixes this.
## How to
The first step is to download the **ng-pubsub** script. You can do it cloning this repo:
```bash
$ git clone https://github.com/vermicida/ng-pubsub.git
```Or via NPM:
```bash
$ npm install ng-pubsub
```Once the library is downloaded, make sure you are referencing it in your `index.html`, just after the AngularJS library reference. The minified version is only recommended for a production environment.
```html```
You must inject the **ng-pubsub** dependency within your module setter:
```javascript
angular.module("test", ["dahr.ng-pubsub"]);
```To subscribe a scope to an event, just use the `$subscribe` function. An unsubscriber function is returned, so remember to keep it safe:
```javascript
angular
.module("test")
.controller("sibling1", function($scope, $pubsub) {
var unsubscribe = $pubsub.$subscribe("onMessagePublished", function(data) {
alert(data.message);
});
}
);
```Then, to publish data on that event use `$publish`:
```javascript
angular
.module("test")
.controller("sibling2", function($scope, $pubsub) {
$pubsub.$publish("onMessagePublished", {
message: "Hi, world"
});
}
);
```If you want to unsubscribe the scope from that event, just do the following:
```javascript
unsubscribe();
```## License
Code released under the [MIT license](./LICENSE).