Advertisement
Capaj

Angular JS async fix

Jan 11th, 2013
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * @license AngularJS v"NG_VERSION_FULL"
  3.  * (c) 2010-2012 Google, Inc. http://angularjs.org
  4.  * License: MIT
  5.  */
  6. 'use strict';
  7. (
  8. /**
  9.  * @ngdoc interface
  10.  * @name angular.Module
  11.  * @description
  12.  *
  13.  * Interface for configuring angular {@link angular.module modules}.
  14.  */
  15.  
  16. function setupModuleLoader(window) {
  17.  
  18.     function ensure(obj, name, factory) {
  19.         return obj[name] || (obj[name] = factory());
  20.     }
  21.  
  22.     return ensure(ensure(window, 'angular', Object), 'module', function() {
  23.         /** @type {Object.<string, angular.Module>} */
  24.         var modules = {};
  25.  
  26.         /**
  27.          * @ngdoc function
  28.          * @name angular.module
  29.          * @description
  30.          *
  31.          * The `angular.module` is a global place for creating and registering Angular modules. All
  32.          * modules (angular core or 3rd party) that should be available to an application must be
  33.          * registered using this mechanism.
  34.          *
  35.          *
  36.          * # Module
  37.          *
  38.          * A module is a collocation of services, directives, filters, and configuration information. Module
  39.          * is used to configure the {@link AUTO.$injector $injector}.
  40.          *
  41.          * <pre>
  42.          * // Create a new module
  43.          * var myModule = angular.module('myModule', []);
  44.          *
  45.          * // register a new service
  46.          * myModule.value('appName', 'MyCoolApp');
  47.          *
  48.          * // configure existing services inside initialization blocks.
  49.          * myModule.config(function($locationProvider) {
  50.      *   // Configure existing providers
  51.      *   $locationProvider.hashPrefix('!');
  52.      * });
  53.          * </pre>
  54.          *
  55.          * Then you can create an injector and load your modules like this:
  56.          *
  57.          * <pre>
  58.          * var injector = angular.injector(['ng', 'MyModule'])
  59.          * </pre>
  60.          *
  61.          * However it's more likely that you'll just use
  62.          * {@link ng.directive:ngApp ngApp} or
  63.          * {@link angular.bootstrap} to simplify this process for you.
  64.          *
  65.          * @param {!string} name The name of the module to create or retrieve.
  66.          * @param {Array.<string>=} requires If specified then new module is being created. If unspecified then the
  67.          *        the module is being retrieved for further configuration.
  68.          * @param {Function} configFn Optional configuration function for the module. Same as
  69.          *        {@link angular.Module#config Module#config()}.
  70.          * @returns {module} new module with the {@link angular.Module} api.
  71.          */
  72.         return function module(name, requires, configFn) {
  73.             if (requires && modules.hasOwnProperty(name)) {
  74.                 modules[name] = null;
  75.             }
  76.             return ensure(modules, name, function() {
  77.                 if (!requires) {
  78.                     throw Error('No module: ' + name);
  79.                 }
  80.  
  81.                 /** @type {!Array.<Array.<*>>} */
  82.                 var invokeQueue = [];
  83.  
  84.                 /** @type {!Array.<Function>} */
  85.                 var runBlocks = [];
  86.  
  87.                 var config = invokeLater('$injector', 'invoke');
  88.  
  89.                 /** @type {angular.Module} */
  90.                 var moduleInstance = {
  91.                     // Private state
  92.                     _invokeQueue: invokeQueue,
  93.                     _runBlocks: runBlocks,
  94.  
  95.                     /**
  96.                      * @ngdoc property
  97.                      * @name angular.Module#requires
  98.                      * @propertyOf angular.Module
  99.                      * @returns {Array.<string>} List of module names which must be loaded before this module.
  100.                      * @description
  101.                      * Holds the list of modules which the injector will load before the current module is loaded.
  102.                      */
  103.                     requires: requires,
  104.  
  105.                     /**
  106.                      * @ngdoc property
  107.                      * @name angular.Module#name
  108.                      * @propertyOf angular.Module
  109.                      * @returns {string} Name of the module.
  110.                      * @description
  111.                      */
  112.                     name: name,
  113.  
  114.  
  115.                     /**
  116.                      * @ngdoc method
  117.                      * @name angular.Module#provider
  118.                      * @methodOf angular.Module
  119.                      * @param {string} name service name
  120.                      * @param {Function} providerType Construction function for creating new instance of the service.
  121.                      * @description
  122.                      * See {@link AUTO.$provide#provider $provide.provider()}.
  123.                      */
  124.                     provider: invokeLater('$provide', 'provider'),
  125.  
  126.                     /**
  127.                      * @ngdoc method
  128.                      * @name angular.Module#factory
  129.                      * @methodOf angular.Module
  130.                      * @param {string} name service name
  131.                      * @param {Function} providerFunction Function for creating new instance of the service.
  132.                      * @description
  133.                      * See {@link AUTO.$provide#factory $provide.factory()}.
  134.                      */
  135.                     factory: invokeLater('$provide', 'factory'),
  136.  
  137.                     /**
  138.                      * @ngdoc method
  139.                      * @name angular.Module#service
  140.                      * @methodOf angular.Module
  141.                      * @param {string} name service name
  142.                      * @param {Function} constructor A constructor function that will be instantiated.
  143.                      * @description
  144.                      * See {@link AUTO.$provide#service $provide.service()}.
  145.                      */
  146.                     service: invokeLater('$provide', 'service'),
  147.  
  148.                     /**
  149.                      * @ngdoc method
  150.                      * @name angular.Module#value
  151.                      * @methodOf angular.Module
  152.                      * @param {string} name service name
  153.                      * @param {*} object Service instance object.
  154.                      * @description
  155.                      * See {@link AUTO.$provide#value $provide.value()}.
  156.                      */
  157.                     value: invokeLater('$provide', 'value'),
  158.  
  159.                     /**
  160.                      * @ngdoc method
  161.                      * @name angular.Module#constant
  162.                      * @methodOf angular.Module
  163.                      * @param {string} name constant name
  164.                      * @param {*} object Constant value.
  165.                      * @description
  166.                      * Because the constant are fixed, they get applied before other provide methods.
  167.                      * See {@link AUTO.$provide#constant $provide.constant()}.
  168.                      */
  169.                     constant: invokeLater('$provide', 'constant', 'unshift'),
  170.  
  171.                     /**
  172.                      * @ngdoc method
  173.                      * @name angular.Module#filter
  174.                      * @methodOf angular.Module
  175.                      * @param {string} name Filter name.
  176.                      * @param {Function} filterFactory Factory function for creating new instance of filter.
  177.                      * @description
  178.                      * See {@link ng.$filterProvider#register $filterProvider.register()}.
  179.                      */
  180.                     filter: invokeLater('$filterProvider', 'register'),
  181.  
  182.                     /**
  183.                      * @ngdoc method
  184.                      * @name angular.Module#controller
  185.                      * @methodOf angular.Module
  186.                      * @param {string} name Controller name.
  187.                      * @param {Function} constructor Controller constructor function.
  188.                      * @description
  189.                      * See {@link ng.$controllerProvider#register $controllerProvider.register()}.
  190.                      */
  191.                     controller: invokeLater('$controllerProvider', 'register'),
  192.  
  193.                     /**
  194.                      * @ngdoc method
  195.                      * @name angular.Module#directive
  196.                      * @methodOf angular.Module
  197.                      * @param {string} name directive name
  198.                      * @param {Function} directiveFactory Factory function for creating new instance of
  199.                      * directives.
  200.                      * @description
  201.                      * See {@link ng.$compileProvider#directive $compileProvider.directive()}.
  202.                      */
  203.                     directive: invokeLater('$compileProvider', 'directive'),
  204.  
  205.                     /**
  206.                      * @ngdoc method
  207.                      * @name angular.Module#config
  208.                      * @methodOf angular.Module
  209.                      * @param {Function} configFn Execute this function on module load. Useful for service
  210.                      *    configuration.
  211.                      * @description
  212.                      * Use this method to register work which needs to be performed on module loading.
  213.                      */
  214.                     config: config,
  215.  
  216.                     /**
  217.                      * @ngdoc method
  218.                      * @name angular.Module#run
  219.                      * @methodOf angular.Module
  220.                      * @param {Function} initializationFn Execute this function after injector creation.
  221.                      *    Useful for application initialization.
  222.                      * @description
  223.                      * Use this method to register work which should be performed when the injector is done
  224.                      * loading all modules.
  225.                      */
  226.                     run: function(block) {
  227.                         runBlocks.push(block);
  228.                         return this;
  229.                     }
  230.                 };
  231.  
  232.                 if (configFn) {
  233.                     config(configFn);
  234.                 }
  235.  
  236.                 return  moduleInstance;
  237.  
  238.                 /**
  239.                  * @param {string} provider
  240.                  * @param {string} method
  241.                  * @param {String=} insertMethod
  242.                  * @returns {angular.Module}
  243.                  */
  244.                 function invokeLater(provider, method, insertMethod) {
  245.                     return function() {
  246.                         invokeQueue[insertMethod || 'push']([provider, method, arguments]);
  247.                         return moduleInstance;
  248.                     }
  249.                 }
  250.             });
  251.         };
  252.     });
  253.  
  254. })(window);
  255.  
  256. /**
  257.  * Closure compiler type information
  258.  *
  259.  * @typedef { {
  260.  *   requires: !Array.<string>,
  261.  *   invokeQueue: !Array.<Array.<*>>,
  262.  *
  263.  *   service: function(string, Function):angular.Module,
  264.  *   factory: function(string, Function):angular.Module,
  265.  *   value: function(string, *):angular.Module,
  266.  *
  267.  *   filter: function(string, Function):angular.Module,
  268.  *
  269.  *   init: function(Function):angular.Module
  270.  * } }
  271.  */
  272. angular.Module;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement