angular.js/src/loader.js

277 lines
9.7 KiB
JavaScript
Raw Normal View History

2012-01-07 02:10:47 +00:00
'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
*
2012-01-17 20:13:48 +00:00
* The `angular.module` is a global place for creating and registering Angular modules. All
* modules (angular core or 3rd party) that should be available to an application must be
* registered using this mechanism.
2012-01-16 07:28:10 +00:00
*
2012-01-07 02:10:47 +00:00
*
* # Module
*
* A module is a collocation of services, directives, filters, and configuration information. Module
* is used to configure the {@link AUTO.$injector $injector}.
2012-01-07 02:10:47 +00:00
*
* <pre>
* // Create a new module
* var myModule = angular.module('myModule', []);
*
2012-01-16 07:28:10 +00:00
* // register a new service
2012-01-07 02:10:47 +00:00
* myModule.value('appName', 'MyCoolApp');
*
* // configure existing services inside initialization blocks.
2012-01-16 07:28:10 +00:00
* myModule.config(function($locationProvider) {
2012-01-07 02:10:47 +00:00
* // Configure existing providers
2012-01-16 07:28:10 +00:00
* $locationProvider.hashPrefix('!');
2012-01-07 02:10:47 +00:00
* });
* </pre>
*
2012-01-16 07:28:10 +00:00
* Then you can create an injector and load your modules like this:
2012-01-07 02:10:47 +00:00
*
* <pre>
* var injector = angular.injector(['ng', 'MyModule'])
2012-01-07 02:10:47 +00:00
* </pre>
*
* However it's more likely that you'll just use
* {@link ng.directive:ngApp ngApp} or
2012-01-16 07:28:10 +00:00
* {@link angular.bootstrap} to simplify this process for you.
*
2012-01-07 02:10:47 +00:00
* @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 Optional configuration function for the module. Same as
2012-01-16 07:28:10 +00:00
* {@link angular.Module#config Module#config()}.
2012-01-17 20:13:48 +00:00
* @returns {module} new module with the {@link angular.Module} api.
2012-01-07 02:10:47 +00:00
*/
2012-01-13 22:19:10 +00:00
return function module(name, requires, configFn) {
2012-01-07 02:10:47 +00:00
if (requires && modules.hasOwnProperty(name)) {
modules[name] = null;
}
return ensure(modules, name, function() {
if (!requires) {
throw minErr('$injector')('nomod', "Module '{0}' is not available! You either misspelled the module name or forgot to load it.", name);
2012-01-07 02:10:47 +00:00
}
/** @type {!Array.<Array.<*>>} */
var invokeQueue = [];
2012-01-13 22:19:10 +00:00
/** @type {!Array.<Function>} */
var runBlocks = [];
var config = invokeLater('$injector', 'invoke');
2012-01-07 02:10:47 +00:00
/** @type {angular.Module} */
var moduleInstance = {
2012-01-13 22:19:10 +00:00
// Private state
_invokeQueue: invokeQueue,
_runBlocks: runBlocks,
2012-01-07 02:10:47 +00:00
/**
* @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,
2012-01-13 22:19:10 +00:00
/**
* @ngdoc property
* @name angular.Module#name
* @propertyOf angular.Module
* @returns {string} Name of the module.
* @description
*/
name: name,
2012-01-07 02:10:47 +00:00
/**
* @ngdoc method
* @name angular.Module#provider
2012-01-07 02:10:47 +00:00
* @methodOf angular.Module
* @param {string} name service name
* @param {Function} providerType Construction function for creating new instance of the service.
* @description
* See {@link AUTO.$provide#provider $provide.provider()}.
2012-01-07 02:10:47 +00:00
*/
provider: invokeLater('$provide', 'provider'),
2012-01-07 02:10:47 +00:00
/**
* @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 AUTO.$provide#factory $provide.factory()}.
2012-01-07 02:10:47 +00:00
*/
factory: invokeLater('$provide', 'factory'),
/**
* @ngdoc method
* @name angular.Module#service
* @methodOf angular.Module
* @param {string} name service name
* @param {Function} constructor A constructor function that will be instantiated.
* @description
* See {@link AUTO.$provide#service $provide.service()}.
*/
service: invokeLater('$provide', 'service'),
2012-01-07 02:10:47 +00:00
/**
* @ngdoc method
* @name angular.Module#value
* @methodOf angular.Module
* @param {string} name service name
* @param {*} object Service instance object.
* @description
* See {@link AUTO.$provide#value $provide.value()}.
2012-01-07 02:10:47 +00:00
*/
value: invokeLater('$provide', 'value'),
2012-02-22 21:28:42 +00:00
/**
* @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 AUTO.$provide#constant $provide.constant()}.
2012-02-22 21:28:42 +00:00
*/
constant: invokeLater('$provide', 'constant', 'unshift'),
/**
* @ngdoc method
* @name angular.Module#animation
* @methodOf angular.Module
* @param {string} name animation name
* @param {Function} animationFactory Factory function for creating new instance of an animation.
* @description
*
* Defines an animation hook that can be later used with {@link ng.directive:ngAnimate ngAnimate}
* alongside {@link ng.directive:ngAnimate#Description common ng directives} as well as custom directives.
* <pre>
* module.animation('animation-name', function($inject1, $inject2) {
* return {
* //this gets called in preparation to setup an animation
* setup : function(element) { ... },
*
* //this gets called once the animation is run
* start : function(element, done, memo) { ... }
* }
* })
* </pre>
*
* See {@link ng.$animationProvider#register $animationProvider.register()} and
* {@link ng.directive:ngAnimate ngAnimate} for more information.
*/
animation: invokeLater('$animationProvider', 'register'),
2012-01-07 02:10:47 +00:00
/**
* @ngdoc method
* @name angular.Module#filter
* @methodOf angular.Module
* @param {string} name Filter name.
2012-01-07 02:10:47 +00:00
* @param {Function} filterFactory Factory function for creating new instance of filter.
* @description
* See {@link ng.$filterProvider#register $filterProvider.register()}.
2012-01-07 02:10:47 +00:00
*/
filter: invokeLater('$filterProvider', 'register'),
/**
* @ngdoc method
* @name angular.Module#controller
* @methodOf angular.Module
* @param {string} name Controller name.
* @param {Function} constructor Controller constructor function.
* @description
* See {@link ng.$controllerProvider#register $controllerProvider.register()}.
*/
controller: invokeLater('$controllerProvider', 'register'),
/**
* @ngdoc method
* @name angular.Module#directive
* @methodOf angular.Module
* @param {string} name directive name
* @param {Function} directiveFactory Factory function for creating new instance of
* directives.
* @description
* See {@link ng.$compileProvider#directive $compileProvider.directive()}.
*/
directive: invokeLater('$compileProvider', 'directive'),
2012-01-07 02:10:47 +00:00
/**
* @ngdoc method
2012-01-13 22:19:10 +00:00
* @name angular.Module#config
2012-01-07 02:10:47 +00:00
* @methodOf angular.Module
2012-01-16 07:28:10 +00:00
* @param {Function} configFn Execute this function on module load. Useful for service
* configuration.
2012-01-07 02:10:47 +00:00
* @description
* Use this method to register work which needs to be performed on module loading.
*/
2012-01-13 22:19:10 +00:00
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 should be performed when the injector is done
* loading all modules.
2012-01-13 22:19:10 +00:00
*/
run: function(block) {
runBlocks.push(block);
return this;
}
2012-01-07 02:10:47 +00:00
};
2012-01-13 22:19:10 +00:00
if (configFn) {
config(configFn);
2012-01-07 02:10:47 +00:00
}
return moduleInstance;
/**
* @param {string} provider
* @param {string} method
2012-02-22 21:28:42 +00:00
* @param {String=} insertMethod
2012-01-07 02:10:47 +00:00
* @returns {angular.Module}
*/
2012-02-22 21:28:42 +00:00
function invokeLater(provider, method, insertMethod) {
2012-01-07 02:10:47 +00:00
return function() {
2012-02-22 21:28:42 +00:00
invokeQueue[insertMethod || 'push']([provider, method, arguments]);
2012-01-07 02:10:47 +00:00
return moduleInstance;
}
}
});
};
});
}