npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ng-pub-sub

v1.1.1

Published

An AngularJS module that uses the publish/subscribe pattern allowing your controllers to listen for events in your factories/directives/etc

Downloads

64

Readme

ng-pub-sub

Build Status

An AngularJS module that uses the publish/subscribe pattern allowing your controllers to listen for events in your factories/directives/controllers/etc. You get a single provider: $pubSub.

How this compares to other solutions

There's a few options out there that are quite similar. Here's what makes ng-pub-sub unique.

  • No need to inject into controller - Use the convenience method $extend to extend your existing services. That means there's no need to inject $pubSub into your controller.

  • Cleans up after itself - Other solutions require you to listen for your controllers $destroy event so you can manually unsubscribe. ng-pub-sub cleans up after itself. Awesome!

  • Clean, well tested codebase

Install

Bower

bower install ng-pub-sub

Usage

Require ng-pub-sub and inject your custom services

angular.module('myApp', [
    'ng-pub-sub'
]).controller('MainCtrl', function(
    $scope,
    userService
){});

Inject $pubSub into your service

.factory('userService', function($pubSub)

Extend your service using $extend

return $pubSub.$extend(service);

Publish ($emit)

$pubSub.$emit('getUsers', userData);

Subscribe ($on)

userService.$on('getUsers', function(users) {
  vm.users = users;
}, $scope);

See it in action | Demo

Guide

Here's everything ng-pub-sub can do

$extend (not required)

This gives you the ability to extend existing services. This means you don't have to inject $pubSub into your controllers. It's just a way of further simplifying your controllers.

With $extend

angular.module('myApp', [
    'ng-pub-sub'
]).controller('MainCtrl', function(
    $scope,
    userService
){
    userService.$on('getUsers', function(users) {
      vm.users = users;
    }, $scope);
});

Without $extend

angular.module('myApp', [
    'ng-pub-sub'
]).controller('MainCtrl', function(
    $scope,
    $pubSub,
    userService
){
    $pubSub.$on('getUsers', function(users) {
      vm.users = users;
    }, $scope);
});

$on

Creates a new subscriber. This acts as a listener, waiting for the message to be sent. You can create multiple subscriptions with the same name. Each subscription returns and id, which you can use to unsubscribe individual subscriptions.

You have the option of providing $scope. This is only necessary if you want ng-pub-sub to clean up subscriptions when the controller is destryed. You can clean up subscriptions manually if required (see $off).

$on with $scope

angular.module('myApp', [
    'ng-pub-sub'
]).controller('MainCtrl', function(
    $scope,
    userService
){
    var id = userService.$on('getUsers', function(users) {
      vm.users = users;
    }, $scope);
});

$on without $scope

angular.module('myApp', [
    'ng-pub-sub'
]).controller('MainCtrl', function(
    $scope,
    $pubSub,
    userService
){
    userService.$on('getUsers', function(users) {
      vm.users = users;
    });

    $scope.$on('$destroy', function() {
        $pubSub.off('getUsers');
    });
});

$once

Just like $on except it only fires once. After that, the subscription is removed.

$emit

Publishes a message to all subscribers.

angular.module('myApp', [
    'ng-pub-sub'
]).factory('userService', function($pubSub) {
  return {
    getUsers: function() {
        $pubSub.$emit('getUsers', [{ id: 1, name: 'Phillip' }, { id: 1, name: 'Jane' }]);
    }
  }
});

$off

Removes subscriptions. There's 3 ways to do this:

  1. Provide the subscription id - a single subscription will be removed
  2. Provide a subscription name - all subscriptions of that name will be removed
  3. Provide a scope - all subscriptions from that scope will be removed.

$off with id

angular.module('myApp', [
    'ng-pub-sub'
]).controller('MainCtrl', function(
    $scope,
    userService
){
    var id = userService.$on('getUsers', function(users) {
      vm.users = users;
    }, $scope);

    userService.$off(id);
});

$off with name

angular.module('myApp', [
    'ng-pub-sub'
]).controller('MainCtrl', function(
    $scope,
    userService
){
    userService.$on('getUsers', function(users) {
      vm.users = users;
    }, $scope);

    userService.$off('getUsers');
});

$off with $scope

angular.module('myApp', [
    'ng-pub-sub'
]).controller('MainCtrl', function(
    $scope,
    userService
){
    userService.$on('getUsers', function(users) {
      vm.users = users;
    }, $scope);

    userService.$off($scope);
});

$callbacks

Gets all callback functions given the name of the subscription or the scope associated with the subscription. Pass nothing to get all callbacks.

angular.module('myApp', [
    'ng-pub-sub'
]).controller('MainCtrl', function(
    $scope,
    userService
){
    userService.$on('getUsers', function(users) {
      vm.users = users;
    }, $scope);

    var namedCallbacks = userService.$callbacks('getUsers');
    var scopedCallbacks = userService.$callbacks($scope);
    var allCallbacks = userService.$callbacks();
});