Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 85b7d24

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 8469779 commit 85b7d24

File tree

1 file changed

+64
-26
lines changed

1 file changed

+64
-26
lines changed

src/auto/injector.js

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -260,39 +260,76 @@ function annotate(fn) {
260260
* a service. The Provider can have additional methods which would allow for configuration of the provider.
261261
*
262262
* <pre>
263-
* function GreetProvider() {
264-
* var salutation = 'Hello';
265-
*
266-
* this.salutation = function(text) {
267-
* salutation = text;
268-
* };
269-
*
270-
* this.$get = function() {
271-
* return function (name) {
272-
* return salutation + ' ' + name + '!';
263+
* function TrackingProvider() {
264+
* this.$get = function($http) {
265+
* var observed = {};
266+
* return {
267+
* event: function(event) {
268+
* var current = observed[event];
269+
* return observed[event] = current ? current + 1 : 1;
270+
* },
271+
* save: function() {
272+
* $http.post("/track",observed);
273+
* }
273274
* };
274275
* };
275276
* }
276277
*
277-
* describe('Greeter', function(){
278-
*
278+
* describe('Tracking', function() {
279+
* var mocked;
279280
* beforeEach(module(function($provide) {
280-
* $provide.provider('greet', GreetProvider);
281+
* $provide.provider('tracking', TrackingProvider);
282+
* mocked = {post: jasmine.createSpy('postSpy')};
283+
* $provide.value('$http',mocked);
281284
* }));
282-
*
283-
* it('should greet', inject(function(greet) {
284-
* expect(greet('angular')).toEqual('Hello angular!');
285+
* it('allows events to be tracked', inject(function(tracking) {
286+
* expect(tracking.event('login')).toEqual(1);
287+
* expect(tracking.event('login')).toEqual(2);
285288
* }));
286289
*
287-
* it('should allow configuration of salutation', function() {
288-
* module(function(greetProvider) {
289-
* greetProvider.salutation('Ahoj');
290-
* });
291-
* inject(function(greet) {
292-
* expect(greet('angular')).toEqual('Ahoj angular!');
293-
* });
294-
* });
290+
* it('posts to save', inject(function(tracking) {
291+
* tracking.save();
292+
* expect(mocked.post.callCount).toEqual(1);
293+
* }));
294+
* });
295295
* </pre>
296+
*
297+
* There are also shorthand methods to define services that don't need to be configured beyond their `$get()` method.
298+
*
299+
* `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.
300+
*
301+
* <pre>
302+
* function TrackingProvider($http) {
303+
* var observed = {};
304+
* this.event = function(event) {
305+
* var current = observed[event];
306+
* return observed[event] = current ? current + 1 : 1;
307+
* };
308+
* this.save = function() {
309+
* $http.post("/track",observed);
310+
* };
311+
* }
312+
* $provider.service('tracking',TrackingProvider);
313+
* </pre>
314+
*
315+
* `factory()` registers a function whose return value is the instance. Again, you can specify services that will be provided by the injector.
316+
*
317+
* <pre>
318+
* function TrackingProvider($http) {
319+
* var observed = {};
320+
* return {
321+
* event: function(event) {
322+
* var current = observed[event];
323+
* return observed[event] = current ? current + 1 : 1;
324+
* },
325+
* save: function() {
326+
* $http.post("/track",observed);
327+
* }
328+
* };
329+
* }
330+
* $provider.factory('tracking',TrackingProvider);
331+
* </pre>
332+
*
296333
*/
297334

298335
/**
@@ -320,7 +357,7 @@ function annotate(fn) {
320357
* @methodOf AUTO.$provide
321358
* @description
322359
*
323-
* A short hand for configuring services if only `$get` method is required.
360+
* A service whose instance is the return value of `$getFn`. Short hand for configuring services if only `$get` method is required.
324361
*
325362
* @param {string} name The name of the instance.
326363
* @param {function()} $getFn The $getFn for the instance creation. Internally this is a short hand for
@@ -335,7 +372,7 @@ function annotate(fn) {
335372
* @methodOf AUTO.$provide
336373
* @description
337374
*
338-
* A short hand for registering service of given class.
375+
* A service whose instance is created by invoking `constructor` with `new`. A short hand for registering services which use a constructor.
339376
*
340377
* @param {string} name The name of the instance.
341378
* @param {Function} constructor A class (constructor function) that will be instantiated.
@@ -602,3 +639,4 @@ function createInjector(modulesToLoad) {
602639
};
603640
}
604641
}
642+

0 commit comments

Comments
 (0)