Last active
January 22, 2019 05:04
-
-
Save hiddentao/7300694 to your computer and use it in GitHub Desktop.
An improvement on the angular.module() API, making it easier to split up modules into multiple files without having to worry about only registering them once.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Workaround to make defining and retrieving angular modules easier and more intuitive. | |
*/ | |
(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