angularAMD.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*jslint node: true, vars: true, nomen: true */
  2. /*globals define, angular */
  3. define(function () {
  4. var orig_angular,
  5. alt_angular,
  6. alternate_queue = [],
  7. app_name,
  8. app_injector,
  9. app_cached_providers = {};
  10. // Private method to check if angularAMD has been initialized
  11. function checkAngularAMDInitialized() {
  12. if ( typeof orig_angular === 'undefined' ) {
  13. throw Error("angularAMD not initialized. Need to call angularAMD.bootstrap(app) first.");
  14. }
  15. }
  16. /**
  17. * Create an alternate angular so that subsequent call to angular.module will queue up
  18. * the module created for later processing via the .processQueue method.
  19. *
  20. * This delaying processing is needed as angular does not recognize any newly created
  21. * module after angular.bootstrap has ran. The only way to add new objects to angular
  22. * post bootstrap is using cached provider.
  23. *
  24. * Once the modules has been queued, processQueue would then use each module's _invokeQueue
  25. * and _runBlock to recreate object using cached $provider. In essence, creating a duplicate
  26. * object into the current ng-app. As result, if there are subsequent call to retrieve the
  27. * module post processQueue, it would retrieve a module that is not integrated into the ng-app.
  28. *
  29. * Therefore, any subsequent angular.module call to retrieve the module created with alternate
  30. * angular will return undefined.
  31. *
  32. */
  33. function setAlternateAngular() {
  34. var alternateModules = {};
  35. // This method cannot be called more than once
  36. if (alt_angular) {
  37. throw Error("setAlternateAngular can only be called once.");
  38. } else {
  39. alt_angular = {};
  40. }
  41. // Make sure that bootstrap has been called
  42. checkAngularAMDInitialized();
  43. // Createa a copy of orig_angular
  44. orig_angular.extend(alt_angular, orig_angular);
  45. // Custom version of angular.module used as cache
  46. alt_angular.module = function (name, requires) {
  47. if (typeof requires === "undefined") {
  48. // Return undefined if module was created using the alt_angular
  49. if (alternateModules.hasOwnProperty(name)) {
  50. return undefined;
  51. } else {
  52. return orig_angular.module(name);
  53. }
  54. } else {
  55. //console.log("alt_angular.module START for '" + name + "': ", arguments);
  56. var orig_mod = orig_angular.module.apply(null, arguments),
  57. item = { name: name, module: orig_mod};
  58. alternate_queue.push(item);
  59. alternateModules[name] = orig_mod;
  60. return orig_mod;
  61. }
  62. };
  63. window.angular = alt_angular;
  64. };
  65. // Constructor
  66. function angularAMD() {}
  67. /**
  68. * Helper function to generate angular's $routeProvider.route. 'config' input param must be an object.
  69. *
  70. * Populate the resolve attribute using either 'controllerUrl' or 'controller'. If 'controllerUrl'
  71. * is passed, it will attempt to load the Url using requirejs and remove the attribute from the config
  72. * object. Otherwise, it will attempt to populate resolve by loading what's been passed in 'controller'.
  73. * If neither is passed, resolve is not populated.
  74. *
  75. * This function works as a pass-through, meaning what ever is passed in as 'config' will be returned,
  76. * except for 'controllerUrl' attribute.
  77. *
  78. */
  79. angularAMD.prototype.route = function (config) {
  80. // Initialization not necessary to call this method.
  81. var load_controller;
  82. /*
  83. If controllerUrl is provided, load the provided Url using requirejs. Otherwise,
  84. attempt to load the controller using the controller name. In the later case,
  85. controller name is expected to be defined as one of 'paths' in main.js.
  86. */
  87. if ( config.hasOwnProperty("controllerUrl") ) {
  88. load_controller = config.controllerUrl;
  89. delete config.controllerUrl;
  90. } else if (typeof config.controller === 'string') {
  91. load_controller = config.controller;
  92. }
  93. // If controller needs to be loaded, append to the resolve property
  94. if (load_controller) {
  95. var resolve = config.resolve || {};
  96. resolve['__load'] = ['$q', '$rootScope', function ($q, $rootScope) {
  97. var defer = $q.defer();
  98. require([load_controller], function () {
  99. defer.resolve();
  100. $rootScope.$apply();
  101. });
  102. return defer.promise;
  103. }]
  104. config.resolve = resolve;
  105. }
  106. return config;
  107. };
  108. /**
  109. * Expose name of the app that has been bootstraped
  110. */
  111. angularAMD.prototype.appname = function () {
  112. checkAngularAMDInitialized();
  113. return app_name;
  114. };
  115. /**
  116. * Recreate the modules created by alternate angular in ng-app using cached $provider.
  117. * As AMD loader does not guarantee the order of dependency in a require([...],...)
  118. * clause, user must make sure that dependecies are clearly setup in shim in order
  119. * for this to work.
  120. *
  121. * HACK ALERT:
  122. * This method relay on inner working of angular.module code, and access _invokeQueue
  123. * and _runBlock private variable. Must test carefully with each release of angular.
  124. */
  125. angularAMD.prototype.processQueue = function () {
  126. checkAngularAMDInitialized();
  127. if (typeof alt_angular === 'undefined') {
  128. throw Error("Alternate angular not set. Make sure that `enable_ngload` option has been set when calling angularAMD.bootstrap");
  129. }
  130. // Process alternate queue in FIFO fashion
  131. while (alternate_queue.length) {
  132. var item = alternate_queue.shift(),
  133. invokeQueue = item.module._invokeQueue,
  134. y;
  135. // Setup the providers define in the module
  136. for (y = 0; y < invokeQueue.length; y += 1) {
  137. var q = invokeQueue[y],
  138. provider = q[0],
  139. method = q[1],
  140. args = q[2];
  141. if (app_cached_providers.hasOwnProperty(provider)) {
  142. var cachedProvider = app_cached_providers[provider];
  143. //console.log("'" + item.name + "': applying " + provider + "." + method + " for args: ", args);
  144. cachedProvider[method].apply(null, args);
  145. } else {
  146. console.error("'" + provider + "' not found!!!");
  147. }
  148. }
  149. // Execute the run block of the module
  150. if (item.module._runBlocks) {
  151. angular.forEach(item.module._runBlocks, function processRunBlock(block) {
  152. //console.log("'" + item.name + "': executing run block: ", run_block);
  153. app_injector.invoke(block);
  154. });
  155. }
  156. // How to remove the module???
  157. orig_angular.module(item.name, [], orig_angular.noop);
  158. }
  159. };
  160. /**
  161. * Return cached app provider
  162. */
  163. angularAMD.prototype.getCachedProvider = function (provider_name) {
  164. checkAngularAMDInitialized();
  165. // Hack used for unit testing that orig_angular has been captured
  166. if (provider_name === "__orig_angular") {
  167. return orig_angular;
  168. } else if (provider_name === "__alt_angular") {
  169. return alt_angular;
  170. } else {
  171. return app_cached_providers[provider_name];
  172. }
  173. };
  174. /**
  175. * Create inject function that uses cached $injector.
  176. * Designed primarly to be used during unit testing.
  177. */
  178. angularAMD.prototype.inject = function () {
  179. checkAngularAMDInitialized();
  180. return app_injector.invoke.apply(null, arguments);
  181. };
  182. /**
  183. * Reset angularAMD for resuse
  184. */
  185. angularAMD.prototype.reset = function () {
  186. if (typeof orig_angular === 'undefined') {
  187. return;
  188. }
  189. // Restore original angular instance
  190. window.angular = orig_angular;
  191. // Clear private variables
  192. alt_angular = undefined;
  193. alternate_queue = [];
  194. app_name = undefined;
  195. app_injector = undefined;
  196. app_cached_providers = {};
  197. // Clear original angular
  198. orig_angular = undefined;
  199. }
  200. /**
  201. * Initialization of angularAMD that bootstraps AngularJS. The objective is to cache the
  202. * $provider and $injector from the app to be used later.
  203. *
  204. * enable_ngload:
  205. */
  206. angularAMD.prototype.bootstrap = function (app, enable_ngload, elem) {
  207. // Prevent bootstrap from being called multiple times
  208. if (typeof orig_angular !== 'undefined') {
  209. throw Error("bootstrap can only be called once.");
  210. }
  211. // Store reference to original angular which also used to check if bootstrap has take place.
  212. orig_angular = angular;
  213. if (typeof enable_ngload === 'undefined') {
  214. enable_ngload = true;
  215. }
  216. elem = elem || document.documentElement;
  217. // Cache provider needed
  218. app.config(
  219. ['$controllerProvider', '$compileProvider', '$filterProvider', '$animateProvider', '$provide', function (controllerProvider, compileProvider, filterProvider, animateProvider, provide) {
  220. // Cache Providers
  221. app_cached_providers = {
  222. $controllerProvider: controllerProvider,
  223. $compileProvider: compileProvider,
  224. $filterProvider: filterProvider,
  225. $animateProvider: animateProvider,
  226. $provide: provide
  227. };
  228. // Create a app.register object
  229. app.register = {
  230. controller: controllerProvider.register,
  231. directive: compileProvider.directive,
  232. filter: filterProvider.register,
  233. factory: provide.factory,
  234. service: provide.service,
  235. constant: provide.constant,
  236. value: provide.value,
  237. animation: animateProvider.register
  238. };
  239. }]
  240. );
  241. // Get the injector for the app
  242. app.run(['$injector', function ($injector) {
  243. // $injector must be obtained in .run instead of .config
  244. app_injector = $injector;
  245. app_cached_providers.$injector = app_injector;
  246. }]);
  247. // Store the app name needed by .bootstrap function.
  248. app_name = app.name;
  249. // Bootstrap Angular
  250. orig_angular.element(document).ready(function () {
  251. orig_angular.bootstrap(elem, [app_name]);
  252. });
  253. // Replace angular.module
  254. if (enable_ngload) {
  255. //console.log("Setting alternate angular");
  256. setAlternateAngular();
  257. }
  258. };
  259. // Create a new instance and return
  260. return new angularAMD();
  261. });