@@ -14,40 +14,46 @@ provided by angular's web framework:
1414
1515<pre>
1616/**
17- * batchLog service allows for messages to be queued in memory and flushed
18- * to the console.log every 50 seconds.
19- *
20- * @param {*} message Message to be logged.
21- */
22- angular.module.ng('batchLog', function($defer, $log) {
23- var messageQueue = [];
24-
25- function log() {
26- if (messageQueue.length) {
27- $log('batchLog messages: ', messageQueue);
28- messageQueue = [];
29- }
30- $defer(log, 50000);
31- }
17+ * batchLog service allows for messages to be queued in memory and flushed
18+ * to the console.log every 50 seconds.
19+ *
20+ * @param {*} message Message to be logged.
21+ */
22+ function batchLogModule($provide){
23+ $provide.factory('batchLog', ['$defer', '$log', function($defer, $log) {
24+ var messageQueue = [];
25+
26+ function log() {
27+ if (messageQueue.length) {
28+ $log('batchLog messages: ', messageQueue);
29+ messageQueue = [];
30+ }
31+ $defer(log, 50000);
32+ }
3233
33- // start periodic checking
34- log();
34+ // start periodic checking
35+ log();
3536
36- return function(message) {
37- messageQueue.push(message);
37+ return function(message) {
38+ messageQueue.push(message);
39+ }
40+ }]);
41+
42+ /**
43+ * routeTemplateMonitor monitors each $route change and logs the current
44+ * template via the batchLog service.
45+ */
46+ $provide.factory('routeTemplateMonitor',
47+ ['$route', 'batchLog', '$rootScope',
48+ function($route, batchLog, $rootScope) {
49+ $rootScope.$on('$afterRouteChange', function() {
50+ batchLog($route.current ? $route.current.template : null);
51+ });
52+ }]);
3853 }
39- }, {$inject: ['$defer', '$log']});
40- // note how we declared dependency on built-in $defer and $log services above
4154
42- /**
43- * routeTemplateMonitor monitors each $route change and logs the current
44- * template via the batchLog service.
45- */
46- angular.module.ng('routeTemplateMonitor', function($route, batchLog) {
47- this.$on('$afterRouteChange', function() {
48- batchLog($route.current ? $route.current.template : null);
49- });
50- }, {$inject: ['$route', 'batchLog'], $eager: true});
55+ // get the main service to kick of the application
56+ angular.injector(batchLogModule).get('routeTemplateMonitor');
5157</pre>
5258
5359Things to notice in this example:
@@ -57,11 +63,6 @@ Things to notice in this example:
5763`console.log` in batches.
5864* The `routeTemplateMonitor` service depends on the built-in {@link api/angular.module.ng.$route
5965$route} service as well as our custom `batchLog` service.
60- * The `routeTemplateMonitor` service is declared to be eager, so that it is started as soon as the
61- application starts.
62- * To underline the need for the eager instantiation of the `routeTemplateMonitor` service, nothing
63- else in the application depends on this service, and in this particular case the factory function
64- of this service doesn't return anything at all.
6566* Both of our services use the factory function signature as well as the `$inject` property to
6667declare their dependencies. It is important that the order of the string identifiers in the array
6768associated with the `$inject` property is the same as the order of argument names in the signature
0 commit comments