del($eager): removed the support for $eager services

This commit is contained in:
Misko Hevery 2011-11-12 14:57:43 -08:00
parent acbd7cdf32
commit 8d6dc0b9a7
7 changed files with 39 additions and 96 deletions

View file

@ -14,9 +14,6 @@ The `angular.module.ng` method accepts three parameters:
- `$inject` - {Array.<string>} - Array of service ids this service depends on. These services
will be passed as arguments into the factory function in the same order specified in the `$inject`
array. Defaults to `[]`.
- `$eager` - {boolean} - If true, the service factory will be called and the service will be
instantiated when angular boots. If false, the service will be lazily instantiated when it is first
requested during instantiation of a dependant. Defaults to `false`.
The `this` of the factory function is bound to the root scope of the angular application.

View file

@ -14,40 +14,46 @@ provided by angular's web framework:
<pre>
/**
* 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.
*/
angular.module.ng('batchLog', function($defer, $log) {
var messageQueue = [];
* 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){
$provide.factory('batchLog', ['$defer', '$log', function($defer, $log) {
var messageQueue = [];
function log() {
if (messageQueue.length) {
$log('batchLog messages: ', messageQueue);
messageQueue = [];
}
$defer(log, 50000);
function log() {
if (messageQueue.length) {
$log('batchLog messages: ', messageQueue);
messageQueue = [];
}
$defer(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('$afterRouteChange', function() {
batchLog($route.current ? $route.current.template : null);
});
}]);
}
// start periodic checking
log();
return function(message) {
messageQueue.push(message);
}
}, {$inject: ['$defer', '$log']});
// note how we declared dependency on built-in $defer and $log services above
/**
* routeTemplateMonitor monitors each $route change and logs the current
* template via the batchLog service.
*/
angular.module.ng('routeTemplateMonitor', function($route, batchLog) {
this.$on('$afterRouteChange', function() {
batchLog($route.current ? $route.current.template : null);
});
}, {$inject: ['$route', 'batchLog'], $eager: true});
// get the main service to kick of the application
angular.injector(batchLogModule).get('routeTemplateMonitor');
</pre>
Things to notice in this example:
@ -57,11 +63,6 @@ Things to notice in this example:
`console.log` in batches.
* The `routeTemplateMonitor` service depends on the built-in {@link api/angular.module.ng.$route
$route} service as well as our custom `batchLog` service.
* The `routeTemplateMonitor` service is declared to be eager, so that it is started as soon as the
application starts.
* To underline the need for the eager instantiation of the `routeTemplateMonitor` service, nothing
else in the application depends on this service, and in this particular case the factory function
of this service doesn't return anything at all.
* Both of our services use the factory function signature as well as the `$inject` property to
declare their dependencies. It is important that the order of the string identifiers in the array
associated with the `$inject` property is the same as the order of argument names in the signature

View file

@ -33,13 +33,9 @@ angular.module.ng('service id', function() {
var shinyNewServiceInstance;
//factory function body that constructs shinyNewServiceInstance
return shinyNewServiceInstance;
}, {$eager: true});
});
</pre>
While it is tempting to declare services as eager, only in few cases it is actually useful. If you
are unsure whether to make a service eager, it likely doesn't need to be. To be more specific, a
service should be declared as eager only if it fits one of these scenarios:
* Nothing in your application declares this service as its dependency, and this service affects the
state or configuration of the application (e.g. a service that configures `$route` or `$resource`
services)

View file

@ -9,7 +9,7 @@
$route.when('/view2', {controller: MyCtrl, template: 'view2.html'});
function MyCtrl() {};
}, {$inject: ['$route'], $eager: true});
}, {$inject: ['$route']});
</script>
</head>
<body ng:init="$service('$window').$root = this">

View file

@ -390,14 +390,5 @@ function createInjector(modulesToLoad, moduleRegistry) {
loadModule(modulesToLoad);
// instantiate $eager providers
// for perf we can't do forEach
for(var name in cache) {
var index = name.indexOf(providerSuffix);
if (index == name.length - providerSuffixLength && cache[name].$eager) {
$injector(name.substring(1, index));
}
}
return $injector;
}

View file

@ -389,18 +389,6 @@ describe('angular', function() {
})('svc2')).toEqual('svc2-svc1');
});
it('should eagerly instantiate a service if $eager is true', function() {
var log = [];
angular.injector(function($provide){
$provide.service('svc1', function() {
this.$get = function(){
log.push('svc1');
}
this.$eager = true;
});
});
expect(log).toEqual(['svc1']);
});
});

View file

@ -67,20 +67,6 @@ describe('injector', function() {
}).toThrow("Unknown provider for 'idontexist' <- 'a' <- 'b'.");
});
it('should autostart eager services', function() {
var log = '';
injector = createInjector([function($provide){
$provide.service('eager', function() {
this.$eager = true;
this.$get = function(){
log += 'eager;';
return 'foo';
};
});
}]);
expect(log).toEqual('eager;');
expect(injector('eager')).toBe('foo');
});
describe('invoke', function() {
var args;
@ -440,20 +426,4 @@ describe('injector', function() {
}).toThrow('MyError');
});
});
describe('$eager', function(){
it('should eagerly instantiate a service if $eager is true', function() {
var log = [];
createInjector([function($provide){
$provide.service('svc1', function() {
this.$get = function(){
log.push('svc1');
}
this.$eager = true;
});
}]);
expect(log).toEqual(['svc1']);
});
});
});