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

Commit 21630bb

Browse files
feat(Module): add info() method
The new `info()` method lets developers store arbitrary information about their module for consumption later.
1 parent 34434cf commit 21630bb

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

docs/content/error/ng/aobj.ngdoc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@ngdoc error
2+
@name ng:aobj
3+
@fullName Invalid Argument
4+
@description
5+
6+
The argument passed should be an object. Check the value that was passed to the function where
7+
this error was thrown.

src/loader.js

+36
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ function setupModuleLoader(window) {
7979
* @returns {angular.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,39 @@ 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+
* You can retrieve this information during runtime via the
138+
* {@link $injector#modules `$injector.modules`} property:
139+
*
140+
* ```js
141+
* var version = $injector.modules['myModule'].info().version;
142+
* ```
143+
*/
144+
info: function(value) {
145+
if (isDefined(value)) {
146+
if (!isObject(value)) throw ngMinErr('aobj', 'Argument \'{0}\' must be an object', 'value');
147+
info = value;
148+
return this;
149+
}
150+
return info;
151+
},
152+
117153
/**
118154
* @ngdoc property
119155
* @name angular.Module#requires

test/loaderSpec.js

+25
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,29 @@ describe('module loader', function() {
156156
it('should expose `$$minErr` on the `angular` object', function() {
157157
expect(window.angular.$$minErr).toEqual(jasmine.any(Function));
158158
});
159+
160+
describe('Module', function() {
161+
describe('info()', function() {
162+
var theModule;
163+
164+
beforeEach(function() {
165+
theModule = angular.module('theModule', []);
166+
});
167+
168+
it('should default to an empty object', function() {
169+
expect(theModule.info()).toEqual({});
170+
});
171+
172+
it('should store the object passed as a param', function() {
173+
theModule.info({ version: '1.2' });
174+
expect(theModule.info()).toEqual({ version: '1.2' });
175+
});
176+
177+
it('should throw if the parameter is not an object', function() {
178+
expect(function() {
179+
theModule.info('some text');
180+
}).toThrowMinErr('ng', 'aobj');
181+
});
182+
});
183+
});
159184
});

0 commit comments

Comments
 (0)