mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-16 23:30:23 +00:00
feat($provide.service): Add $provide.service() for registering a class
This commit is contained in:
parent
00d4427388
commit
0bfaa579c0
4 changed files with 69 additions and 3 deletions
|
|
@ -230,13 +230,27 @@ function inferInjectionArgs(fn) {
|
|||
*
|
||||
* A short hand for configuring services if only `$get` method is required.
|
||||
*
|
||||
* @param {string} name The name of the instance. NOTE: the provider will be available under `name + 'Provider'` key.
|
||||
* @param {string} name The name of the instance.
|
||||
* @param {function()} $getFn The $getFn for the instance creation. Internally this is a short hand for
|
||||
* `$provide.provider(name, {$get: $getFn})`.
|
||||
* @returns {Object} registered provider instance
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @ngdoc method
|
||||
* @name angular.module.AUTO.$provide#service
|
||||
* @methodOf angular.module.AUTO.$provide
|
||||
* @description
|
||||
*
|
||||
* A short hand for registering service of given class.
|
||||
*
|
||||
* @param {string} name The name of the instance.
|
||||
* @param {Function} constructor A class (constructor function) that will be instantiated.
|
||||
* @returns {Object} registered provider instance
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @ngdoc method
|
||||
* @name angular.module.AUTO.$provide#value
|
||||
|
|
@ -245,7 +259,7 @@ function inferInjectionArgs(fn) {
|
|||
*
|
||||
* A short hand for configuring services if the `$get` method is a constant.
|
||||
*
|
||||
* @param {string} name The name of the instance. NOTE: the provider will be available under `name + 'Provider'` key.
|
||||
* @param {string} name The name of the instance.
|
||||
* @param {*} value The value.
|
||||
* @returns {Object} registered provider instance
|
||||
*/
|
||||
|
|
@ -296,6 +310,7 @@ function createInjector(modulesToLoad) {
|
|||
$provide: {
|
||||
provider: supportObject(provider),
|
||||
factory: supportObject(factory),
|
||||
service: supportObject(service),
|
||||
value: supportObject(value),
|
||||
constant: supportObject(constant),
|
||||
decorator: decorator
|
||||
|
|
@ -342,6 +357,12 @@ function createInjector(modulesToLoad) {
|
|||
|
||||
function factory(name, factoryFn) { return provider(name, { $get: factoryFn }); }
|
||||
|
||||
function service(name, constructor) {
|
||||
return factory(name, ['$injector', function($injector) {
|
||||
return $injector.instantiate(constructor);
|
||||
}]);
|
||||
}
|
||||
|
||||
function value(name, value) { return factory(name, valueFn(value)); }
|
||||
|
||||
function constant(name, value) {
|
||||
|
|
|
|||
|
|
@ -125,10 +125,21 @@ function setupModuleLoader(window) {
|
|||
* @param {string} name service name
|
||||
* @param {Function} providerFunction Function for creating new instance of the service.
|
||||
* @description
|
||||
* See {@link angular.module.AUTO.$provide#service $provide.factory()}.
|
||||
* See {@link angular.module.AUTO.$provide#factory $provide.factory()}.
|
||||
*/
|
||||
factory: invokeLater('$provide', 'factory'),
|
||||
|
||||
/**
|
||||
* @ngdoc method
|
||||
* @name angular.Module#service
|
||||
* @methodOf angular.Module
|
||||
* @param {string} name service name
|
||||
* @param {Function} constructor A constructor function that will be instantiated.
|
||||
* @description
|
||||
* See {@link angular.module.AUTO.$provide#service $provide.service()}.
|
||||
*/
|
||||
service: invokeLater('$provide', 'service'),
|
||||
|
||||
/**
|
||||
* @ngdoc method
|
||||
* @name angular.Module#value
|
||||
|
|
|
|||
|
|
@ -315,6 +315,38 @@ describe('injector', function() {
|
|||
});
|
||||
|
||||
|
||||
describe('service', function() {
|
||||
it('should register a class', function() {
|
||||
var Type = function(value) {
|
||||
this.value = value;
|
||||
};
|
||||
|
||||
var instance = createInjector([function($provide) {
|
||||
$provide.value('value', 123);
|
||||
$provide.service('foo', Type);
|
||||
}]).get('foo');
|
||||
|
||||
expect(instance instanceof Type).toBe(true);
|
||||
expect(instance.value).toBe(123);
|
||||
});
|
||||
|
||||
|
||||
it('should register a set of classes', function() {
|
||||
var Type = function() {};
|
||||
|
||||
var injector = createInjector([function($provide) {
|
||||
$provide.service({
|
||||
foo: Type,
|
||||
bar: Type
|
||||
});
|
||||
}]);
|
||||
|
||||
expect(injector.get('foo') instanceof Type).toBe(true);
|
||||
expect(injector.get('bar') instanceof Type).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('provider', function() {
|
||||
it('should configure $provide provider object', function() {
|
||||
expect(createInjector([function($provide) {
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ describe('module loader', function() {
|
|||
expect(myModule.
|
||||
provider('sk', 'sv').
|
||||
factory('fk', 'fv').
|
||||
service('a', 'aa').
|
||||
value('k', 'v').
|
||||
filter('f', 'ff').
|
||||
directive('d', 'dd').
|
||||
|
|
@ -47,6 +48,7 @@ describe('module loader', function() {
|
|||
['$injector', 'invoke', ['config'] ],
|
||||
['$provide', 'provider', ['sk', 'sv'] ],
|
||||
['$provide', 'factory', ['fk', 'fv'] ],
|
||||
['$provide', 'service', ['a', 'aa'] ],
|
||||
['$provide', 'value', ['k', 'v'] ],
|
||||
['$filterProvider', 'register', ['f', 'ff'] ],
|
||||
['$compileProvider', 'directive', ['d', 'dd'] ],
|
||||
|
|
|
|||
Loading…
Reference in a new issue