Skip to content

Instantly share code, notes, and snippets.

@johncblandii
Created April 26, 2013 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johncblandii/5465115 to your computer and use it in GitHub Desktop.
Save johncblandii/5465115 to your computer and use it in GitHub Desktop.
An example Angular channel service. It abstracts the event dispatch/subscribe to make it easier to implement/use. Here are some pub/sub best practices: http://eburley.github.io/2013/01/31/angularjs-watch-pub-sub-best-practices.html.
/**
* AuthenticationChannel
*
* Usage (from within a controller):
AuthenticationChannel.onSetPin($scope, function(pin){
console.log("Pin set to ", pin);
})
//anywhere else in your code base
AuthenticationChannel.setPin(1234567); //set the pin
*/
define(
[],
function() {
var AuthenticationChannel = function($rootScope) {
var setPin = function(pin) {
$rootScope.$broadcast("pinSet", pin);
};
var onSetPin = function($scope, handler) {
$scope.$on("pinSet", function(event, pin){
if(hander && typeof handler === "function")
handler(pin);
});
};
return {
setPin: setPin,
onSetPin: onSetPin
};
};
AuthenticationChannel.$inject = ['$rootScope'];
return AuthenticationChannel;
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment