diff options
| -rw-r--r-- | docs/content/guide/dev_guide.services.creating_services.ngdoc | 3 | ||||
| -rw-r--r-- | docs/content/guide/dev_guide.services.managing_dependencies.ngdoc | 71 | ||||
| -rw-r--r-- | docs/content/guide/dev_guide.services.registering_services.ngdoc | 6 | ||||
| -rw-r--r-- | example/temp.html | 2 | ||||
| -rw-r--r-- | src/Injector.js | 9 | ||||
| -rw-r--r-- | test/AngularSpec.js | 12 | ||||
| -rw-r--r-- | test/InjectorSpec.js | 30 | 
7 files changed, 38 insertions, 95 deletions
| diff --git a/docs/content/guide/dev_guide.services.creating_services.ngdoc b/docs/content/guide/dev_guide.services.creating_services.ngdoc index 397f86c7..06d57d77 100644 --- a/docs/content/guide/dev_guide.services.creating_services.ngdoc +++ b/docs/content/guide/dev_guide.services.creating_services.ngdoc @@ -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. diff --git a/docs/content/guide/dev_guide.services.managing_dependencies.ngdoc b/docs/content/guide/dev_guide.services.managing_dependencies.ngdoc index b3135b83..5ca76654 100644 --- a/docs/content/guide/dev_guide.services.managing_dependencies.ngdoc +++ b/docs/content/guide/dev_guide.services.managing_dependencies.ngdoc @@ -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 = []; - -  function log() { -    if (messageQueue.length) { -      $log('batchLog messages: ', messageQueue); -      messageQueue = []; -    } -    $defer(log, 50000); -  } + * 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); +      } -  // start periodic checking -  log(); +      // start periodic checking +      log(); -  return function(message) { -    messageQueue.push(message); +      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); +      }); +    }]);    } -}, {$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 diff --git a/docs/content/guide/dev_guide.services.registering_services.ngdoc b/docs/content/guide/dev_guide.services.registering_services.ngdoc index 24a21852..25a51504 100644 --- a/docs/content/guide/dev_guide.services.registering_services.ngdoc +++ b/docs/content/guide/dev_guide.services.registering_services.ngdoc @@ -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) diff --git a/example/temp.html b/example/temp.html index 7c46eb7f..41fb4746 100644 --- a/example/temp.html +++ b/example/temp.html @@ -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"> diff --git a/src/Injector.js b/src/Injector.js index fd67a25e..fb64abd1 100644 --- a/src/Injector.js +++ b/src/Injector.js @@ -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;  } diff --git a/test/AngularSpec.js b/test/AngularSpec.js index e14f2e27..4579000d 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -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']); -    });    }); diff --git a/test/InjectorSpec.js b/test/InjectorSpec.js index ac412c39..6c0e9668 100644 --- a/test/InjectorSpec.js +++ b/test/InjectorSpec.js @@ -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']); -    }); -  }); -  }); | 
