Created
December 5, 2013 08:10
-
-
Save congjf/7801799 to your computer and use it in GitHub Desktop.
Angular Service Samples
This file contains hidden or 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
/** | |
* 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