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

angular-bullet

v1.1.7

Published

An ultra lightweight and simple to use pub-sub library.

Downloads

7

Readme

Angular Bullet

Angular Bullet is an ultra lightweight and simple to use pub-sub library, with AMD/module support and an intuitive API. It was built to facilitate a simple and consistent system of communication across AngularJS applications and includes only the bare essentials typically needed to achieve this. The main advantage of Angular Bullet over using $rootScope is that it has an 'off' method, which removes the need to keep track of and deregister event handlers.

Usage

You can install via npm with the following command in your command prompt:

npm install angular-bullet --save

Or you can install via Bower instead, if that's your thing:

bower install angular-bullet

Otherwise, simply grab the source code from Github.

Include Angular Bullet in your application

If using CommonJS then simply require Angular Bullet as per usual, prior to setting up your AngularJS modules:

require("angular-bullet");

Otherwise use a regular script tag:

<script type="application/javascript" src="path/to/angular-bullet.js"></script>

Then ensure that you include Angular Bullet in your module definition:

var CoolApp = angular.module("CoolApp", ["angular-bullet"]);

You can then inject it where necessary, for example:

CoolApp.controller("MainCtrl", ["angularBullet", MainCtrl]);

function MainCtrl (angularBullet)
{
    angularBullet.trigger("hello");
}

API

.on()

angularBullet.on("someMessageName", callback);

Register a callback function to get called whenever the specified message is triggered.

Example usage:

function helloCallback () {
    console.log("hello there :)");
}
 
// Register the 'helloCallback' function to be called whenever the 'hello' message is triggered:
    
angularBullet.on("hello", helloCallback);
    

// Somewhere later in the application...
    
    
// Trigger the 'hello' message – Angular Bullet will call the 'helloCallback' function:
    
angularBullet.trigger("hello");

.off()

angularBullet.off("someMessageName"[, callback]);

Remove either all callback functions or a specific callback function registered against the specified message.

Example usage:

function helloCallback () {
    console.log("hello there :)");
}

function anotherCallback () {
    console.log("hello again :)");
}


angularBullet.on("hello", helloCallback);
angularBullet.on("hello", anotherCallback);


// Somewhere later in the application...


// Trigger the 'hello' message – Angular Bullet will call both the 'helloCallback' and 'anotherCallback' functions:

angularBullet.trigger("hello");


// Remove all callback functions associated with the 'hello' message:
angularBullet.off("hello");

// Attempt to trigger the 'hello' message again – Angular Bullet won't call any functions:
angularBullet.trigger("hello");

Example usage removing a specific callback:

function helloCallback () {
    console.log("hello there :)");
}

function anotherCallback () {
    console.log("hello again :)");
}


angularBullet.on("hello", helloCallback);
angularBullet.on("hello", anotherCallback);


// Somewhere later in the application...


// Trigger the 'hello' message – Angular Bullet will call both the 'helloCallback' and 'anotherCallback' functions:

angularBullet.trigger("hello");


// Remove only the 'anotherCallback' function associated with the 'hello' message:
angularBullet.off("hello", anotherCallback);

// Trigger the 'hello' message again – Angular Bullet will only call the 'helloCallback' function:
angularBullet.trigger("hello");

.once()

angularBullet.once("someMessageName", callback);

This function behaves in the same way as the the 'on' function, except that – once registered – the callback function will only be called a single time when the specified message is triggered.

Example usage:

function helloCallback () {
    console.log("hello there :)");
}


// Register the 'helloCallback' function to be called whenever the 'hello' message is triggered:

angularBullet.once("hello", helloCallback);


// Somewhere later in the application...


// Trigger the 'hello' message – Angular Bullet will call the 'helloCallback' function:

angularBullet.trigger("hello");


// Attempt to trigger the 'hello' message again – Angular Bullet won't call any functions this time:

angularBullet.trigger("hello");

.trigger()

angularBullet.trigger("someMessageName"[, data]);

This function will call all callback functions registered against the specified message, optionally passing in custom data as a payload.

Example usage:

function helloCallback () {
    console.log("hello there :)");
}


// Register the 'helloCallback' function to be called whenever the 'hello' message is triggered:

angularBullet.on("hello", helloCallback);


// Somewhere later in the application...


// Trigger the 'hello' message – Angular Bullet will call the 'helloCallback' function:

angularBullet.trigger("hello");

Example usage with custom data:

function userAddedCallback (data) {
    console.log(data);
}


// Register the 'userAddedCallback' function to be called whenever the 'user-added' message is triggered:

angularBullet.on("user-added", userAddedCallback);


// Somewhere later in the application...


// Create some custom data:

var customData = {
    someProp : "bro",
    someOtherProp : "awesome!"
};


// Trigger the 'user-added' message – Angular Bullet will call the 'helloCallback' function and
// pass in the custom data that you created, which will be sent to the function as a parameter:

angularBullet.trigger("user-added", customData);

Important!

Make sure that you use the 'off' method to de-register any listeners when AngularJS sends a '$destroy' event to a $scope, otherwise Angular Bullet could keep a reference to functions that no longer need to exist, potentially creating a memory leak.

An easy way to do this is to listen for the '$destroy' event and act on it accordingly:

function UserLoginViewController ($scope, angularBullet)
{
    angularBullet.on("someEventName", functionA);
    angularBullet.on("anotherEventName", functionB);

    $scope.$on("$destroy", destroyHandler);

    function functionA ()
    {
        // code...
    }

    function functionB ()
    {
        // code...
    }

    function destroyHandler ()
    {
        angularBullet.off("someEventName", functionA);
        angularBullet.off("anotherEventName", functionB);
    }
}

About the Project

Angular Bullet was developed and is currently maintained by Ivan Hayes.

Head over to the Github project page to find out more. Go ahead and star the project to keep up with its latest developments :-)