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!');
    });

  });

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.