Skip to content

Instantly share code, notes, and snippets.

@aslong
Created July 19, 2012 22:53
Show Gist options
  • Save aslong/3147427 to your computer and use it in GitHub Desktop.
Save aslong/3147427 to your computer and use it in GitHub Desktop.
Cyclic Node Require - Handle node require with cyclic references
_ = 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