0.0.2 • Published 9 years ago

angular-channel v0.0.2

Weekly downloads
-
License
-
Repository
-
Last release
9 years ago

Angular Channels

This is very much a proof of concept/work in progress and could do with improvements.

Example Usage

Create an event channel service

angular.module('myApp.security', ['mouki.channel'])
  .service('SecurityChannel', function($rootScope, EventChannel) {

    var serviceChannel = new EventChannel("SecurityChannel");
    
    // Return an object that has the method logout and onLogout
    return serviceChannel.build(['logout']);
  });

Create a custom event channel service

angular.module('myApp.security', ['mouki.channel'])
  .service('SecurityChannel', function($rootScope, EventChannel) {

    var serviceChannel = new EventChannel("SecurityChannel");
    var _LOGOUT_EVENT_ = 'logout';

    return {
      logout: function (data) {
        serviceChannel.emit(_LOGOUT_EVENT_, data);
      },
      onLogout: function ($scope, handler) {
        return serviceChannel.register($scope, _LOGOUT_EVENT_, handler);
      }
    }
  });

angular.module('myApp.security').service('SecurityService', function(SecurityChannel) {
    return {
      logout: function() {
        // Do actual logout call
        // Notify
        SecurityChannel.logout();
      }
    };
  });

Using the channel

angular.module('myApp.view1', ['ngRoute', 'myApp.security'])

.controller('View1Ctrl', function($scope, $log, SecurityChannel, SecurityService) {

    $scope.logout = function() {
      SecurityService.logout();
    };

    var onLogoutCleanup = SecurityChannel.onLogout($scope, function(item){
      $log.info("Logged out!");
    });

    $scope.$on('$destroy', function() {
      $log.debug("Removing handler");
      onLogoutCleanup();
    });
});

Thanks :)

Based on the work of Eric Burley and Jim Lavin.

Initial Gruntfile.js borrowed from ngStorage :)