Skip to content

Instantly share code, notes, and snippets.

@Torthu
Forked from hiddentao/gist:7300694
Last active August 29, 2015 14:08
Show Gist options
  • Save Torthu/a0fea86616d968a706f6 to your computer and use it in GitHub Desktop.
Save Torthu/a0fea86616d968a706f6 to your computer and use it in GitHub Desktop.
Make Angular module definition independent of file order.
/**
* Workaround to make defining and retrieving angular modules easier and more intuitive.
* Original: https://gist.github.com/hiddentao/7300694 // http://www.hiddentao.com/archives/2013/11/04/an-improved-angular-module-split-your-modules-into-multiple-files/
*/
(function(angular) {
var origMethod = angular.module;
var alreadyRegistered = {};
/**
* Register/fetch a module.
*
* @param name {string} module name.
* @param reqs {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.
*/
angular.module = function(name, reqs, configFn) {
reqs = reqs || [];
var module = null;
if (alreadyRegistered[name]) {
module = origMethod(name);
module.requires.push.apply(module.requires, reqs);
} else {
module = origMethod(name, reqs, configFn);
alreadyRegistered[name] = module;
}
return module;
};
})(angular);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment