Created
April 26, 2013 04:59
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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