mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-09 15:24:43 +00:00
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
This commit is contained in:
parent
8469779a8e
commit
85b7d24357
1 changed files with 64 additions and 26 deletions
|
|
@ -260,39 +260,76 @@ function annotate(fn) {
|
||||||
* a service. The Provider can have additional methods which would allow for configuration of the provider.
|
* a service. The Provider can have additional methods which would allow for configuration of the provider.
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* function GreetProvider() {
|
* function TrackingProvider() {
|
||||||
* var salutation = 'Hello';
|
* this.$get = function($http) {
|
||||||
*
|
* var observed = {};
|
||||||
* this.salutation = function(text) {
|
* return {
|
||||||
* salutation = text;
|
* event: function(event) {
|
||||||
* };
|
* var current = observed[event];
|
||||||
*
|
* return observed[event] = current ? current + 1 : 1;
|
||||||
* this.$get = function() {
|
* },
|
||||||
* return function (name) {
|
* save: function() {
|
||||||
* return salutation + ' ' + name + '!';
|
* $http.post("/track",observed);
|
||||||
|
* }
|
||||||
* };
|
* };
|
||||||
* };
|
* };
|
||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
* describe('Greeter', function(){
|
* describe('Tracking', function() {
|
||||||
*
|
* var mocked;
|
||||||
* beforeEach(module(function($provide) {
|
* beforeEach(module(function($provide) {
|
||||||
* $provide.provider('greet', GreetProvider);
|
* $provide.provider('tracking', TrackingProvider);
|
||||||
|
* mocked = {post: jasmine.createSpy('postSpy')};
|
||||||
|
* $provide.value('$http',mocked);
|
||||||
|
* }));
|
||||||
|
* it('allows events to be tracked', inject(function(tracking) {
|
||||||
|
* expect(tracking.event('login')).toEqual(1);
|
||||||
|
* expect(tracking.event('login')).toEqual(2);
|
||||||
* }));
|
* }));
|
||||||
*
|
*
|
||||||
* it('should greet', inject(function(greet) {
|
* it('posts to save', inject(function(tracking) {
|
||||||
* expect(greet('angular')).toEqual('Hello angular!');
|
* tracking.save();
|
||||||
|
* expect(mocked.post.callCount).toEqual(1);
|
||||||
* }));
|
* }));
|
||||||
*
|
* });
|
||||||
* it('should allow configuration of salutation', function() {
|
|
||||||
* module(function(greetProvider) {
|
|
||||||
* greetProvider.salutation('Ahoj');
|
|
||||||
* });
|
|
||||||
* inject(function(greet) {
|
|
||||||
* expect(greet('angular')).toEqual('Ahoj angular!');
|
|
||||||
* });
|
|
||||||
* });
|
|
||||||
* </pre>
|
* </pre>
|
||||||
|
*
|
||||||
|
* There are also shorthand methods to define services that don't need to be configured beyond their `$get()` method.
|
||||||
|
*
|
||||||
|
* `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.
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* function TrackingProvider($http) {
|
||||||
|
* var observed = {};
|
||||||
|
* this.event = function(event) {
|
||||||
|
* var current = observed[event];
|
||||||
|
* return observed[event] = current ? current + 1 : 1;
|
||||||
|
* };
|
||||||
|
* this.save = function() {
|
||||||
|
* $http.post("/track",observed);
|
||||||
|
* };
|
||||||
|
* }
|
||||||
|
* $provider.service('tracking',TrackingProvider);
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* `factory()` registers a function whose return value is the instance. Again, you can specify services that will be provided by the injector.
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* function TrackingProvider($http) {
|
||||||
|
* var observed = {};
|
||||||
|
* return {
|
||||||
|
* event: function(event) {
|
||||||
|
* var current = observed[event];
|
||||||
|
* return observed[event] = current ? current + 1 : 1;
|
||||||
|
* },
|
||||||
|
* save: function() {
|
||||||
|
* $http.post("/track",observed);
|
||||||
|
* }
|
||||||
|
* };
|
||||||
|
* }
|
||||||
|
* $provider.factory('tracking',TrackingProvider);
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -320,7 +357,7 @@ function annotate(fn) {
|
||||||
* @methodOf AUTO.$provide
|
* @methodOf AUTO.$provide
|
||||||
* @description
|
* @description
|
||||||
*
|
*
|
||||||
* A short hand for configuring services if only `$get` method is required.
|
* A service whose instance is the return value of `$getFn`. Short hand for configuring services if only `$get` method is required.
|
||||||
*
|
*
|
||||||
* @param {string} name The name of the instance.
|
* @param {string} name The name of the instance.
|
||||||
* @param {function()} $getFn The $getFn for the instance creation. Internally this is a short hand for
|
* @param {function()} $getFn The $getFn for the instance creation. Internally this is a short hand for
|
||||||
|
|
@ -335,7 +372,7 @@ function annotate(fn) {
|
||||||
* @methodOf AUTO.$provide
|
* @methodOf AUTO.$provide
|
||||||
* @description
|
* @description
|
||||||
*
|
*
|
||||||
* A short hand for registering service of given class.
|
* A service whose instance is created by invoking `constructor` with `new`. A short hand for registering services which use a constructor.
|
||||||
*
|
*
|
||||||
* @param {string} name The name of the instance.
|
* @param {string} name The name of the instance.
|
||||||
* @param {Function} constructor A class (constructor function) that will be instantiated.
|
* @param {Function} constructor A class (constructor function) that will be instantiated.
|
||||||
|
|
@ -602,3 +639,4 @@ function createInjector(modulesToLoad) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue