Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 3eb2fbf

Browse files
timrufflespetebacondarwin
authored andcommitted
docs($provide): improve examples and explanations
$provide's example seems awkward. Replace with more real-world example, using an injected service, where the service defined has a good reason to be a singleton. There's quite a lot of confusion around $provide: http://stackoverflow.com/search?q=angularjs+service+vs+factory Tests for example at: http://jsbin.com/EMabAv/1/edit?js,output
1 parent cbd69db commit 3eb2fbf

File tree

1 file changed

+64
-26
lines changed

1 file changed

+64
-26
lines changed

src/auto/injector.js

+64-26
Original file line numberDiff line numberDiff line change
@@ -276,39 +276,76 @@ function annotate(fn) {
276276
* a service. The Provider can have additional methods which would allow for configuration of the provider.
277277
*
278278
* <pre>
279-
* function GreetProvider() {
280-
* var salutation = 'Hello';
281-
*
282-
* this.salutation = function(text) {
283-
* salutation = text;
284-
* };
285-
*
286-
* this.$get = function() {
287-
* return function (name) {
288-
* return salutation + ' ' + name + '!';
279+
* function TrackingProvider() {
280+
* this.$get = function($http) {
281+
* var observed = {};
282+
* return {
283+
* event: function(event) {
284+
* var current = observed[event];
285+
* return observed[event] = current ? current + 1 : 1;
286+
* },
287+
* save: function() {
288+
* $http.post("/track",observed);
289+
* }
289290
* };
290291
* };
291292
* }
292293
*
293-
* describe('Greeter', function(){
294-
*
294+
* describe('Tracking', function() {
295+
* var mocked;
295296
* beforeEach(module(function($provide) {
296-
* $provide.provider('greet', GreetProvider);
297+
* $provide.provider('tracking', TrackingProvider);
298+
* mocked = {post: jasmine.createSpy('postSpy')};
299+
* $provide.value('$http',mocked);
297300
* }));
298-
*
299-
* it('should greet', inject(function(greet) {
300-
* expect(greet('angular')).toEqual('Hello angular!');
301+
* it('allows events to be tracked', inject(function(tracking) {
302+
* expect(tracking.event('login')).toEqual(1);
303+
* expect(tracking.event('login')).toEqual(2);
301304
* }));
302305
*
303-
* it('should allow configuration of salutation', function() {
304-
* module(function(greetProvider) {
305-
* greetProvider.salutation('Ahoj');
306-
* });
307-
* inject(function(greet) {
308-
* expect(greet('angular')).toEqual('Ahoj angular!');
309-
* });
310-
* });
306+
* it('posts to save', inject(function(tracking) {
307+
* tracking.save();
308+
* expect(mocked.post.callCount).toEqual(1);
309+
* }));
310+
* });
311311
* </pre>
312+
*
313+
* There are also shorthand methods to define services that don't need to be configured beyond their `$get()` method.
314+
*
315+
* `service()` registers a constructor function which will be invoked with `new` to create the instance. You can specify services that will be provided by the injector.
316+
*
317+
* <pre>
318+
* function TrackingProvider($http) {
319+
* var observed = {};
320+
* this.event = function(event) {
321+
* var current = observed[event];
322+
* return observed[event] = current ? current + 1 : 1;
323+
* };
324+
* this.save = function() {
325+
* $http.post("/track",observed);
326+
* };
327+
* }
328+
* $provider.service('tracking',TrackingProvider);
329+
* </pre>
330+
*
331+
* `factory()` registers a function whose return value is the instance. Again, you can specify services that will be provided by the injector.
332+
*
333+
* <pre>
334+
* function TrackingProvider($http) {
335+
* var observed = {};
336+
* return {
337+
* event: function(event) {
338+
* var current = observed[event];
339+
* return observed[event] = current ? current + 1 : 1;
340+
* },
341+
* save: function() {
342+
* $http.post("/track",observed);
343+
* }
344+
* };
345+
* }
346+
* $provider.factory('tracking',TrackingProvider);
347+
* </pre>
348+
*
312349
*/
313350

314351
/**
@@ -336,7 +373,7 @@ function annotate(fn) {
336373
* @methodOf AUTO.$provide
337374
* @description
338375
*
339-
* A short hand for configuring services if only `$get` method is required.
376+
* A service whose instance is the return value of `$getFn`. Short hand for configuring services if only `$get` method is required.
340377
*
341378
* @param {string} name The name of the instance.
342379
* @param {function()} $getFn The $getFn for the instance creation. Internally this is a short hand for
@@ -351,7 +388,7 @@ function annotate(fn) {
351388
* @methodOf AUTO.$provide
352389
* @description
353390
*
354-
* A short hand for registering service of given class.
391+
* A service whose instance is created by invoking `constructor` with `new`. A short hand for registering services which use a constructor.
355392
*
356393
* @param {string} name The name of the instance.
357394
* @param {Function} constructor A class (constructor function) that will be instantiated.
@@ -621,3 +658,4 @@ function createInjector(modulesToLoad) {
621658
};
622659
}
623660
}
661+

0 commit comments

Comments
 (0)