feat(Module): add info() method
The new `info()` method lets developers store arbitrary information about their module for consumption later. Closes #15225
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
@ngdoc error
|
||||
@name ng:aobj
|
||||
@fullName Invalid Argument
|
||||
@description
|
||||
|
||||
The argument passed should be an object. Check the value that was passed to the function where
|
||||
this error was thrown.
|
||||
@@ -79,6 +79,9 @@ function setupModuleLoader(window) {
|
||||
* @returns {angular.Module} new module with the {@link angular.Module} api.
|
||||
*/
|
||||
return function module(name, requires, configFn) {
|
||||
|
||||
var info = {};
|
||||
|
||||
var assertNotHasOwnProperty = function(name, context) {
|
||||
if (name === 'hasOwnProperty') {
|
||||
throw ngMinErr('badname', 'hasOwnProperty is not a valid {0} name', context);
|
||||
@@ -114,6 +117,45 @@ function setupModuleLoader(window) {
|
||||
_configBlocks: configBlocks,
|
||||
_runBlocks: runBlocks,
|
||||
|
||||
/**
|
||||
* @ngdoc method
|
||||
* @name angular.Module#info
|
||||
* @module ng
|
||||
*
|
||||
* @param {Object=} info Information about the module
|
||||
* @returns {Object|Module} The current info object for this module if called as a getter,
|
||||
* or `this` if called as a setter.
|
||||
*
|
||||
* @description
|
||||
* Read and write custom information about this module.
|
||||
* For example you could put the version of the module in here.
|
||||
*
|
||||
* ```js
|
||||
* angular.module('myModule', []).info({ version: '1.0.0' });
|
||||
* ```
|
||||
*
|
||||
* The version could then be read back out by accessing the module elsewhere:
|
||||
*
|
||||
* ```
|
||||
* var version = angular.module('myModule').info().version;
|
||||
* ```
|
||||
*
|
||||
* You can also retrieve this information during runtime via the
|
||||
* {@link $injector#modules `$injector.modules`} property:
|
||||
*
|
||||
* ```js
|
||||
* var version = $injector.modules['myModule'].info().version;
|
||||
* ```
|
||||
*/
|
||||
info: function(value) {
|
||||
if (isDefined(value)) {
|
||||
if (!isObject(value)) throw ngMinErr('aobj', 'Argument \'{0}\' must be an object', 'value');
|
||||
info = value;
|
||||
return this;
|
||||
}
|
||||
return info;
|
||||
},
|
||||
|
||||
/**
|
||||
* @ngdoc property
|
||||
* @name angular.Module#requires
|
||||
|
||||
@@ -156,4 +156,35 @@ describe('module loader', function() {
|
||||
it('should expose `$$minErr` on the `angular` object', function() {
|
||||
expect(window.angular.$$minErr).toEqual(jasmine.any(Function));
|
||||
});
|
||||
|
||||
describe('Module', function() {
|
||||
describe('info()', function() {
|
||||
var theModule;
|
||||
|
||||
beforeEach(function() {
|
||||
theModule = angular.module('theModule', []);
|
||||
});
|
||||
|
||||
it('should default to an empty object', function() {
|
||||
expect(theModule.info()).toEqual({});
|
||||
});
|
||||
|
||||
it('should store the object passed as a param', function() {
|
||||
theModule.info({ version: '1.2' });
|
||||
expect(theModule.info()).toEqual({ version: '1.2' });
|
||||
});
|
||||
|
||||
it('should throw if the parameter is not an object', function() {
|
||||
expect(function() {
|
||||
theModule.info('some text');
|
||||
}).toThrowMinErr('ng', 'aobj');
|
||||
});
|
||||
|
||||
it('should completely replace the previous info object', function() {
|
||||
theModule.info({ value: 'X' });
|
||||
theModule.info({ newValue: 'Y' });
|
||||
expect(theModule.info()).toEqual({ newValue: 'Y' });
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user