Skip to content

Instantly share code, notes, and snippets.

@congjf
Created December 5, 2013 08:10
Show Gist options
  • Save congjf/7801799 to your computer and use it in GitHub Desktop.
Save congjf/7801799 to your computer and use it in GitHub Desktop.
Angular Service Samples
/**
* batchLog service allows for messages to be queued in memory and flushed
* to the console.log every 50 seconds.
*
* @param {*} message Message to be logged.
*/
function batchLogModule([$provide](http://docs.angularjs.org/api/AUTO.$provide)){
$provide.factory('batchLog', ['$timeout', '$log', function($timeout, $log) {
var messageQueue = [];
function log() {
if (messageQueue.length) {
$log('batchLog messages: ', messageQueue);
messageQueue = [];
}
$timeout(log, 50000);
}
// start periodic checking
log();
return function(message) {
messageQueue.push(message);
}
}]);
/**
* routeTemplateMonitor monitors each $route change and logs the current
* template via the batchLog service.
*/
$provide.factory('routeTemplateMonitor',
['$route', 'batchLog', '$rootScope',
function($route, batchLog, $rootScope) {
$rootScope.$on('$routeChangeSuccess', function() {
batchLog($route.current ? $route.current.template : null);
});
}]);
}
// get the main service to kick of the application
angular.injector([batchLogModule]).get('routeTemplateMonitor');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment