Skip to content

Instantly share code, notes, and snippets.

@apaatsio
Created July 17, 2013 09:52
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 apaatsio/6019247 to your computer and use it in GitHub Desktop.
Save apaatsio/6019247 to your computer and use it in GitHub Desktop.
Load JavaScript functions synchronously just-in-time. This is just a concept. This is not ready and I haven't even tried running this.
myObject =
myFunction: loadFunction 'myModule/myFunction', this
loadFunction = (path, self) ->
(args...) ->
functionDoesntExist = not window.myNamespace?.myModule?.myFunction?
if functionDoesntExist
scriptSrc = loadScriptSrc path
scriptTag = """<script type="text/javascript">#{scriptSrc}</script>"""
document.write scriptTag
# TODO bind self
window.myNamespace.myModule.myFunction args...
loadScriptSrc = (path) ->
pathElements = path.split('/')
# TODO get rid of dependency on _
_.reduce(
pathElements
(currentPath, pathElement) -> currentPath[pathElement]
loadableFunctions
)
loadableFunctions =
myModule:
myFunction: "window.myNamespace.myModule.myFunction = function() {};"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment