mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
201 lines
6.5 KiB
JavaScript
201 lines
6.5 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* @ngdoc interface
|
|
* @name angular.Module
|
|
* @description
|
|
*
|
|
* Interface for configuring angular {@link angular.module modules}.
|
|
*/
|
|
|
|
function setupModuleLoader(window) {
|
|
|
|
function ensure(obj, name, factory) {
|
|
return obj[name] || (obj[name] = factory());
|
|
}
|
|
|
|
return ensure(ensure(window, 'angular', Object), 'module', function() {
|
|
/** @type {Object.<string, angular.Module>} */
|
|
var modules = {};
|
|
|
|
/**
|
|
* @ngdoc function
|
|
* @name angular.module
|
|
* @description
|
|
*
|
|
* The `angular.module` is a global place for registering angular modules. All modules
|
|
* (angular core or 3rd party) that should be available to an application must be registered
|
|
* using this mechanism.
|
|
*
|
|
*
|
|
* # Module
|
|
*
|
|
* A module is a collocation of services, directives, filters, and configure information. Module
|
|
* is used to configure the {@link angular.module.AUTO.$injector $injector}.
|
|
*
|
|
* <pre>
|
|
* // Create a new module
|
|
* var myModule = angular.module('myModule', []);
|
|
*
|
|
* // register a new service
|
|
* myModule.value('appName', 'MyCoolApp');
|
|
*
|
|
* // configure existing services inside initialization blocks.
|
|
* myModule.config(function($locationProvider) {
|
|
* // Configure existing providers
|
|
* $locationProvider.hashPrefix('!');
|
|
* });
|
|
* </pre>
|
|
*
|
|
* Then you can create an injector and load your modules like this:
|
|
*
|
|
* <pre>
|
|
* var injector = angular.injector(['ng', 'MyModule'])
|
|
* </pre>
|
|
*
|
|
* However it's more likely that you'll just use {@link angular.directive.ng:app ng:app} or
|
|
* {@link angular.bootstrap} to simplify this process for you.
|
|
*
|
|
* @param {!string} name The name of the module to create or retrieve.
|
|
* @param {Array.<string>=} requires If specified then new module is being created. If unspecified then the
|
|
* the module is being retrieved for further configuration.
|
|
* @param {Function} configFn Option configuration function for the module. Same as
|
|
* {@link angular.Module#config Module#config()}.
|
|
* @return {angular.Module}
|
|
*/
|
|
return function module(name, requires, configFn) {
|
|
if (requires && modules.hasOwnProperty(name)) {
|
|
modules[name] = null;
|
|
}
|
|
return ensure(modules, name, function() {
|
|
if (!requires) {
|
|
throw Error('No module: ' + name);
|
|
}
|
|
|
|
/** @type {!Array.<Array.<*>>} */
|
|
var invokeQueue = [];
|
|
|
|
/** @type {!Array.<Function>} */
|
|
var runBlocks = [];
|
|
|
|
var config = invokeLater('$injector', 'invoke');
|
|
|
|
/** @type {angular.Module} */
|
|
var moduleInstance = {
|
|
// Private state
|
|
_invokeQueue: invokeQueue,
|
|
_runBlocks: runBlocks,
|
|
|
|
/**
|
|
* @ngdoc property
|
|
* @name angular.Module#requires
|
|
* @propertyOf angular.Module
|
|
* @returns {Array.<string>} List of module names which must be loaded before this module.
|
|
* @description
|
|
* Holds the list of modules which the injector will load before the current module is loaded.
|
|
*/
|
|
requires: requires,
|
|
|
|
/**
|
|
* @ngdoc property
|
|
* @name angular.Module#name
|
|
* @propertyOf angular.Module
|
|
* @returns {string} Name of the module.
|
|
* @description
|
|
*/
|
|
name: name,
|
|
|
|
|
|
/**
|
|
* @ngdoc method
|
|
* @name angular.Module#service
|
|
* @methodOf angular.Module
|
|
* @param {string} name service name
|
|
* @param {Function} providerType Construction function for creating new instance of the service.
|
|
* @description
|
|
* See {@link angular.module.AUTO.$provide#service $provide.service()}.
|
|
*/
|
|
service: invokeLater('$provide', 'service'),
|
|
|
|
/**
|
|
* @ngdoc method
|
|
* @name angular.Module#factory
|
|
* @methodOf angular.Module
|
|
* @param {string} name service name
|
|
* @param {Function} providerFunction Function for creating new instance of the service.
|
|
* @description
|
|
* See {@link angular.module.AUTO.$provide#service $provide.factory()}.
|
|
*/
|
|
factory: invokeLater('$provide', 'factory'),
|
|
|
|
/**
|
|
* @ngdoc method
|
|
* @name angular.Module#value
|
|
* @methodOf angular.Module
|
|
* @param {string} name service name
|
|
* @param {*} object Service instance object.
|
|
* @description
|
|
* See {@link angular.module.AUTO.$provide#value $provide.value()}.
|
|
*/
|
|
value: invokeLater('$provide', 'value'),
|
|
|
|
/**
|
|
* @ngdoc method
|
|
* @name angular.Module#filter
|
|
* @methodOf angular.Module
|
|
* @param {string} name filterr name
|
|
* @param {Function} filterFactory Factory function for creating new instance of filter.
|
|
* @description
|
|
* See {@link angular.module.ng.$filterProvider#register $filterProvider.register()}.
|
|
*/
|
|
filter: invokeLater('$filterProvider', 'register'),
|
|
|
|
/**
|
|
* @ngdoc method
|
|
* @name angular.Module#config
|
|
* @methodOf angular.Module
|
|
* @param {Function} configFn Execute this function on module load. Useful for service
|
|
* configuration.
|
|
* @description
|
|
* Use this method to register work which needs to be performed on module loading.
|
|
*/
|
|
config: config,
|
|
|
|
/**
|
|
* @ngdoc method
|
|
* @name angular.Module#run
|
|
* @methodOf angular.Module
|
|
* @param {Function} initializationFn Execute this function after injector creation.
|
|
* Useful for application initialization.
|
|
* @description
|
|
* Use this method to register work which needs to be performed when the injector with
|
|
* with the current module is finished loading.
|
|
*/
|
|
run: function(block) {
|
|
runBlocks.push(block);
|
|
return this;
|
|
}
|
|
};
|
|
|
|
if (configFn) {
|
|
config(configFn);
|
|
}
|
|
|
|
return moduleInstance;
|
|
|
|
/**
|
|
* @param {string} provider
|
|
* @param {string} method
|
|
* @returns {angular.Module}
|
|
*/
|
|
function invokeLater(provider, method) {
|
|
return function() {
|
|
invokeQueue.push([provider, method, arguments]);
|
|
return moduleInstance;
|
|
}
|
|
}
|
|
});
|
|
};
|
|
});
|
|
|
|
}
|