Skip to content

Instantly share code, notes, and snippets.

@richardvanbergen
Created August 27, 2014 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richardvanbergen/45c833b7bdf40cf274c0 to your computer and use it in GitHub Desktop.
Save richardvanbergen/45c833b7bdf40cf274c0 to your computer and use it in GitHub Desktop.
Method to improve Angular.js's handling of module definitions without modifying angular itself.
(function(window, angular) {
'use strict';
/**
* Register or fetch a module. Angular doesn't handle modules in this way usually, overriding the module instead of
* extending it. So controllers/services/directives/etc can't share the same namespace without separating the
* module definition from the module implementation, leading to files with one line in them or one file of module
* definitions.
*
* @param name {string} Module name.
* @param dependencies {Array} List of modules this module depends upon.
* @param configFn {function} Config function to run when module loads (only applied for the first call to
* create this module).
*
* @returns {*} The created/existing module.
*/
window.module = function(name, dependencies, configFn) {
var module;
try {
module = angular.module(name);
} catch(e) {
module = angular.module(name, dependencies, configFn);
}
return module;
};
})(window, angular);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment