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

Commit

Permalink
feat($provide): added constant
Browse files Browse the repository at this point in the history
  • Loading branch information
mhevery committed Feb 22, 2012
1 parent c27a56f commit 80edcad
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
25 changes: 23 additions & 2 deletions src/Injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,27 @@ 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 + 'Provide'` key.
* @param {function()} value The $getFn for the instance creation. Internally this is a short hand for
* `$provide.service(name, {$get:function(){ return value; }})`.
* @param {*} value The value.
* @returns {Object} registered provider instance
*/


/**
* @ngdoc method
* @name angular.module.AUTO.$provide#constant
* @methodOf angular.module.AUTO.$provide
* @description
*
* A constant value, but unlike {@link angular.module.AUTO.$provide#value value} it can be injected
* into configuration function (other modules) and it is not interceptable by
* {@link angular.module.AUTO.$provide#decorator decorator}.
*
* @param {string} name The name of the constant.
* @param {*} value The constant value.
* @returns {Object} registered instance
*/


/**
* @ngdoc method
* @name angular.module.AUTO.$provide#decorator
Expand Down Expand Up @@ -282,6 +297,7 @@ function createInjector(modulesToLoad) {
service: supportObject(service),
factory: supportObject(factory),
value: supportObject(value),
constant: supportObject(constant),
decorator: decorator
}
},
Expand Down Expand Up @@ -328,6 +344,11 @@ function createInjector(modulesToLoad) {

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

function constant(name, value) {
providerCache[name] = value;
instanceCache[name] = value;
}

function decorator(serviceName, decorFn) {
var origProvider = providerInjector.get(serviceName + providerSuffix),
orig$get = origProvider.$get;
Expand Down
17 changes: 15 additions & 2 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ function setupModuleLoader(window) {
*/
value: invokeLater('$provide', 'value'),

/**
* @ngdoc method
* @name angular.Module#constant
* @methodOf angular.Module
* @param {string} name constant name
* @param {*} object Constant value.
* @description
* Because the constant are fixed, they get applied before other provide methods.
* See {@link angular.module.AUTO.$provide#constant $provide.constant()}.
*/
constant: invokeLater('$provide', 'constant', 'unshift'),

/**
* @ngdoc method
* @name angular.Module#filter
Expand Down Expand Up @@ -199,11 +211,12 @@ function setupModuleLoader(window) {
/**
* @param {string} provider
* @param {string} method
* @param {String=} insertMethod
* @returns {angular.Module}
*/
function invokeLater(provider, method) {
function invokeLater(provider, method, insertMethod) {
return function() {
invokeQueue.push([provider, method, arguments]);
invokeQueue[insertMethod || 'push']([provider, method, arguments]);
return moduleInstance;
}
}
Expand Down
23 changes: 23 additions & 0 deletions test/InjectorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,29 @@ describe('injector', function() {
});

describe('$provide', function() {
describe('constant', function() {
it('should create configuration injectable constants', function() {
var log = [];
createInjector([
function($provide){
$provide.constant('abc', 123);
$provide.constant({a: 'A', b:'B'});
return function(a) {
log.push(a);
}
},
function(abc) {
log.push(abc);
return function(b) {
log.push(b);
}
}
]).get('abc');
expect(log).toEqual([123, 'A', 'B']);
});
});


describe('value', function() {
it('should configure $provide values', function() {
expect(createInjector([function($provide) {
Expand Down
2 changes: 2 additions & 0 deletions test/loaderSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ describe('module loader', function() {
filter('f', 'ff').
directive('d', 'dd').
config('init2').
constant('abc', 123).
run('runBlock')).toBe(myModule);

expect(myModule.requires).toEqual(['other']);
expect(myModule._invokeQueue).toEqual([
['$provide', 'constant', ['abc', 123] ],
['$injector', 'invoke', ['config'] ],
['$provide', 'service', ['sk', 'sv'] ],
['$provide', 'factory', ['fk', 'fv'] ],
Expand Down

0 comments on commit 80edcad

Please sign in to comment.