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

Commit 51f8a57

Browse files
feat(angular.info): optionally store and access additional info on modules
This feature allows the developer to store additional meta data about a module when it is defined by calling `mod.info(infoObject)` on the module object. Developers can then access this information by calling `mod.info()` or `angular.info(moduleName)`. See this angular/material#3842 for more background. Closes #12465
1 parent 469b14a commit 51f8a57

File tree

7 files changed

+99
-6
lines changed

7 files changed

+99
-6
lines changed

src/.jshintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
"encodeUriSegment": false,
8282
"encodeUriQuery": false,
8383
"angularInit": false,
84+
"info": false,
8485
"bootstrap": false,
8586
"getTestability": false,
8687
"snake_case": false,

src/Angular.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,25 @@ function angularInit(element, bootstrap) {
15041504
}
15051505
}
15061506

1507+
1508+
/**
1509+
* @ngdoc function
1510+
* @name angular.info
1511+
* @module ng
1512+
* @returns { Object }
1513+
* An object containing the info about a module or an empty object if
1514+
* the module has not been defined.
1515+
* @description
1516+
* Get the info about a given module.
1517+
*/
1518+
function info(moduleName) {
1519+
try {
1520+
return angularModule(moduleName).info();
1521+
} catch (e) {
1522+
return {};
1523+
}
1524+
}
1525+
15071526
/**
15081527
* @ngdoc function
15091528
* @name angular.bootstrap

src/AngularPublic.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ function publishExternalAPI(angular) {
150150
'getTestability': getTestability,
151151
'$$minErr': minErr,
152152
'$$csp': csp,
153-
'reloadWithDebugInfo': reloadWithDebugInfo
153+
'reloadWithDebugInfo': reloadWithDebugInfo,
154+
'info': info
154155
});
155156

156157
angularModule = setupModuleLoader(window);

src/loader.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ function setupModuleLoader(window) {
7979
* @returns {module} new module with the {@link angular.Module} api.
8080
*/
8181
return function module(name, requires, configFn) {
82+
83+
var info = {};
84+
8285
var assertNotHasOwnProperty = function(name, context) {
8386
if (name === 'hasOwnProperty') {
8487
throw ngMinErr('badname', 'hasOwnProperty is not a valid {0} name', context);
@@ -114,6 +117,37 @@ function setupModuleLoader(window) {
114117
_configBlocks: configBlocks,
115118
_runBlocks: runBlocks,
116119

120+
/**
121+
* @ngdoc method
122+
* @name angular.Module#info
123+
* @module ng
124+
*
125+
* @param {Object=} info Information about the module
126+
* @returns {Object|Module} The current info object for this module if called as a getter,
127+
* or `this` if called as a setter.
128+
*
129+
* @description
130+
* Additional info about this module
131+
* For example you could put the version of the module in here.
132+
*
133+
* ```js
134+
* angular.module('myModule', []).info({ version: '1.0.0' });
135+
* ```
136+
*
137+
* The global method `angular.info()` can be used to retrieve this info:
138+
*
139+
* ```js
140+
* var version = angular.info('myModule').version;
141+
* ```
142+
*/
143+
info: function(value) {
144+
if (isDefined(value)) {
145+
info = value;
146+
return this;
147+
}
148+
return info;
149+
},
150+
117151
/**
118152
* @ngdoc property
119153
* @name angular.Module#requires

test/.jshintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"encodeUriSegment": false,
7676
"encodeUriQuery": false,
7777
"angularInit": false,
78+
"info": false,
7879
"bootstrap": false,
7980
"snake_case": false,
8081
"bindJQuery": false,

test/AngularSpec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,21 @@ describe('angular', function() {
16141614
});
16151615
});
16161616

1617+
1618+
describe('info', function() {
1619+
it('should return the additional info for the named module', function() {
1620+
angular.module('a', []).info({some: 'thing'});
1621+
angular.module('b', ['dep'], function configFn() {}).info({other: 'thang'});
1622+
1623+
expect(info('a')).toEqual({some: 'thing'});
1624+
expect(info('b')).toEqual({other: 'thang'});
1625+
});
1626+
1627+
it('should return anh empty object if there is no such module', function() {
1628+
expect(info('no-such-module')).toEqual({});
1629+
});
1630+
});
1631+
16171632
describe('bootstrap', function() {
16181633
it('should bootstrap app', function() {
16191634
var element = jqLite('<div>{{1+2}}</div>');

test/loaderSpec.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@ describe('module loader', function() {
2626

2727

2828
it('should record calls', function() {
29+
function config() {}
30+
function init() {}
31+
function init2() {}
32+
2933
var otherModule = window.angular.module('other', []);
30-
otherModule.config('otherInit');
34+
otherModule.config(init);
3135

32-
var myModule = window.angular.module('my', ['other'], 'config');
36+
var myModule = window.angular.module('my', ['other'], config);
3337

3438
expect(myModule.
3539
decorator('dk', 'dv').
@@ -40,7 +44,7 @@ describe('module loader', function() {
4044
filter('f', 'ff').
4145
directive('d', 'dd').
4246
controller('ctrl', 'ccc').
43-
config('init2').
47+
config(init2).
4448
constant('abc', 123).
4549
run('runBlock')).toBe(myModule);
4650

@@ -57,13 +61,31 @@ describe('module loader', function() {
5761
['$controllerProvider', 'register', ['ctrl', 'ccc']]
5862
]);
5963
expect(myModule._configBlocks).toEqual([
60-
['$injector', 'invoke', ['config']],
61-
['$injector', 'invoke', ['init2']]
64+
['$injector', 'invoke', [config]],
65+
['$injector', 'invoke', [init2]]
6266
]);
6367
expect(myModule._runBlocks).toEqual(['runBlock']);
6468
});
6569

6670

71+
it('should store additional module info', function() {
72+
var myModule = angular.module('myModule', ['dep1']).info({version: '1.0'});
73+
var otherModule = angular.module('otherModule', ['dep2'], function someConfig() {}).info({version: '2.0'});
74+
var thirdModule = angular.module('thirdModule', ['dep2', 'dep3'], [function someConfig() {}]).info({version: '3.0'});
75+
var fourthModule = angular.module('fourthModule', [], [function someConfig() {}]);
76+
77+
expect(myModule.info()).toEqual({version: '1.0'});
78+
expect(otherModule.info()).toEqual({version: '2.0'});
79+
expect(thirdModule.info()).toEqual({version: '3.0'});
80+
expect(fourthModule.info()).toEqual({});
81+
82+
expect(angular.info('myModule')).toEqual({version: '1.0'});
83+
expect(angular.info('otherModule')).toEqual({version: '2.0'});
84+
expect(angular.info('thirdModule')).toEqual({version: '3.0'});
85+
expect(angular.info('fourthModule')).toEqual({});
86+
});
87+
88+
6789
it('should allow module redefinition', function() {
6890
expect(window.angular.module('a', [])).not.toBe(window.angular.module('a', []));
6991
});

0 commit comments

Comments
 (0)