Created
July 19, 2012 22:53
-
-
Save aslong/3147427 to your computer and use it in GitHub Desktop.
Cyclic Node Require - Handle node require with cyclic references
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
_ = require('underscore') | |
### | |
# | |
# requireLater - Just like node's require, but handles performing require with cyclic references. | |
# | |
# module - the module you are currently calling requireLater from | |
# filepath - a filepath just like you would give to node's require | |
# setModuleOn - (optional) the object to set the module.exports on | |
# | |
# Ex: MongoDb = requireLater(module, './mongodb') | |
### | |
exports.requireLater = (module, filePath, setModuleOn) -> | |
if !setModuleOn | |
setModuleOn = {} | |
if !module.loaded | |
_.defer(exports.requireLater, module, filePath, setModuleOn) | |
return setModuleOn | |
else | |
temp = module.require(filePath) | |
_.defaults(setModuleOn, temp) | |
return setModuleOn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment