Giter Club home page Giter Club logo

hey's Introduction

hey!

pub/sub abstraction layer for AngularJS to make managing your events easier

hey aims to make dealing with events in AngularJS a little bit easier when you require more functionality like destroying event listeners or only listening for a particular event one time

listening for events


Listening for events is pretty straight forward, simply use the listen() method and pass in the name of the event and an event handler to be called.

angular.module('example', ['hey'])
  .controller('ExampleCtrl', function (hey) {

    function doSomething () {
      console.log('something happened!');
    }

    hey.listen('contrived.event', doSomething);

  });

listen() simply wraps $rootScope.$on so you can also use hey to manage native angular events as well.

If you'd prefer not to use $rootScope and to use a child $scope instead, you can pass in a $scope as a third optional argument.

angular.module('example', ['hey'])
  .controller('ExampleCtrl', function (hey, $scope) {

    function doSomething () {
      console.log('something happened!');
    }

    hey.listen('contrived.event', doSomething, $scope);

  });

destroying events


One of the more tedious things about using angular's native event system is when you want to destroy an event. This is the real problem hey aims to solve, it allows you to easily destroy events following the same patterns used in popular libraries like jQuery.

angular.module('example', ['hey'])
  .controller('DestroyExampleCtrl', function (hey, $scope) {

    function doSomething () {
      console.log('something happened!');
    }

    hey.listen('contrived.event', doSomething);

    $scope.destroyEvent = function () {
      hey.stop('contrived.event', doSomething);
    };

  });

You can also use it to remove any and all handlers associated with a particular event by simply not passing in an event handler

angular.module('example', ['hey'])
  .controller('DestroyExampleCtrl', function (hey, $scope) {

    function doSomething () {
      console.log('something happened!');
    }

    function doSomethingElse () {
      console.log('something else happened!');
    }

    hey.listen('contrived.event', doSomething);
    hey.listen('contrived.event', doSomethingElse)


    $scope.destroyAllTheThings = function () {
      hey.stop('contrived.event');
    };

  });

once


One convenience I miss from libraries like jQuery/Backbone is the ability to specify that a listener should only listen one time then die

angular.module('example', ['hey'])
  .controller('OnceCtrl', function (hey) {

    hey.once('contrived.event', function () {
      console.log('There can only be one!');
    });

  });

hey's People

Contributors

callmehiphop avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

hey's Issues

Contribute

Hey (pun intended)

I wanted to contribute, but the karma task doesn't seem to run. I couldn't run the dist task.
What i wanted to add is being able to handle 'stop'ping handlers for an event that doesn't exist.

    /**
     * Removes listeners from the scope regardless of the scope to which they were added.
     *
     * If a callback is provided it will only remove that single callback, otherwise all
     * listeners listening to the given event will be removed.
     *
     * @param {string} eventName Name of the even which listeners should be removed
     * @param {function} [callback] Optionally a single callback which should be removed
     */
    function stop(eventName, callback) {
      var group = listeners[eventName];
      var i = group ? group.length - 1 : false;

      function shouldBeDeleted(listener) {
        return callback ? listener.cb === callback : true;
      }

      if(group) {
        for (; i > -1; i--) {
          if (shouldBeDeleted(group[i])) {
            group.splice(i, 1)[0].destroy();
          }
        }
      }
    }

$broadcast

First thanks for taking the time to open source this and support it!

Is there a reason hey only supports $emit() and not also $broadcast()? I recognize that I can throw a $rootScope.$broadcast() but that requires me to inject $rootScope and that's one of the things I am trying to avoid (once it's injected then it's more likely to be abused)...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.