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

Commit 80edcad

Browse files
committed
feat($provide): added constant
1 parent c27a56f commit 80edcad

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-4
lines changed

src/Injector.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,27 @@ function inferInjectionArgs(fn) {
246246
* A short hand for configuring services if the `$get` method is a constant.
247247
*
248248
* @param {string} name The name of the instance. NOTE: the provider will be available under `name + 'Provide'` key.
249-
* @param {function()} value The $getFn for the instance creation. Internally this is a short hand for
250-
* `$provide.service(name, {$get:function(){ return value; }})`.
249+
* @param {*} value The value.
251250
* @returns {Object} registered provider instance
252251
*/
253252

254253

254+
/**
255+
* @ngdoc method
256+
* @name angular.module.AUTO.$provide#constant
257+
* @methodOf angular.module.AUTO.$provide
258+
* @description
259+
*
260+
* A constant value, but unlike {@link angular.module.AUTO.$provide#value value} it can be injected
261+
* into configuration function (other modules) and it is not interceptable by
262+
* {@link angular.module.AUTO.$provide#decorator decorator}.
263+
*
264+
* @param {string} name The name of the constant.
265+
* @param {*} value The constant value.
266+
* @returns {Object} registered instance
267+
*/
268+
269+
255270
/**
256271
* @ngdoc method
257272
* @name angular.module.AUTO.$provide#decorator
@@ -282,6 +297,7 @@ function createInjector(modulesToLoad) {
282297
service: supportObject(service),
283298
factory: supportObject(factory),
284299
value: supportObject(value),
300+
constant: supportObject(constant),
285301
decorator: decorator
286302
}
287303
},
@@ -328,6 +344,11 @@ function createInjector(modulesToLoad) {
328344

329345
function value(name, value) { return factory(name, valueFn(value)); }
330346

347+
function constant(name, value) {
348+
providerCache[name] = value;
349+
instanceCache[name] = value;
350+
}
351+
331352
function decorator(serviceName, decorFn) {
332353
var origProvider = providerInjector.get(serviceName + providerSuffix),
333354
orig$get = origProvider.$get;

src/loader.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ function setupModuleLoader(window) {
140140
*/
141141
value: invokeLater('$provide', 'value'),
142142

143+
/**
144+
* @ngdoc method
145+
* @name angular.Module#constant
146+
* @methodOf angular.Module
147+
* @param {string} name constant name
148+
* @param {*} object Constant value.
149+
* @description
150+
* Because the constant are fixed, they get applied before other provide methods.
151+
* See {@link angular.module.AUTO.$provide#constant $provide.constant()}.
152+
*/
153+
constant: invokeLater('$provide', 'constant', 'unshift'),
154+
143155
/**
144156
* @ngdoc method
145157
* @name angular.Module#filter
@@ -199,11 +211,12 @@ function setupModuleLoader(window) {
199211
/**
200212
* @param {string} provider
201213
* @param {string} method
214+
* @param {String=} insertMethod
202215
* @returns {angular.Module}
203216
*/
204-
function invokeLater(provider, method) {
217+
function invokeLater(provider, method, insertMethod) {
205218
return function() {
206-
invokeQueue.push([provider, method, arguments]);
219+
invokeQueue[insertMethod || 'push']([provider, method, arguments]);
207220
return moduleInstance;
208221
}
209222
}

test/InjectorSpec.js

+23
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,29 @@ describe('injector', function() {
260260
});
261261

262262
describe('$provide', function() {
263+
describe('constant', function() {
264+
it('should create configuration injectable constants', function() {
265+
var log = [];
266+
createInjector([
267+
function($provide){
268+
$provide.constant('abc', 123);
269+
$provide.constant({a: 'A', b:'B'});
270+
return function(a) {
271+
log.push(a);
272+
}
273+
},
274+
function(abc) {
275+
log.push(abc);
276+
return function(b) {
277+
log.push(b);
278+
}
279+
}
280+
]).get('abc');
281+
expect(log).toEqual([123, 'A', 'B']);
282+
});
283+
});
284+
285+
263286
describe('value', function() {
264287
it('should configure $provide values', function() {
265288
expect(createInjector([function($provide) {

test/loaderSpec.js

+2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ describe('module loader', function() {
3838
filter('f', 'ff').
3939
directive('d', 'dd').
4040
config('init2').
41+
constant('abc', 123).
4142
run('runBlock')).toBe(myModule);
4243

4344
expect(myModule.requires).toEqual(['other']);
4445
expect(myModule._invokeQueue).toEqual([
46+
['$provide', 'constant', ['abc', 123] ],
4547
['$injector', 'invoke', ['config'] ],
4648
['$provide', 'service', ['sk', 'sv'] ],
4749
['$provide', 'factory', ['fk', 'fv'] ],

0 commit comments

Comments
 (0)