Is there a way to have some code run in the browser after coffeescript has compiled and run all the script files in the header?
Something like:
coffee.onCompiled () -> console.log “I’ve finished loading all the coffee files”
How else can I get this behaviour? (please don’t suggest setTimeout)
At first I thought you were asking about how to attach a callback to
window.onloadfrom CoffeeScript (which is, of course, very straightforward, since “It’s just JavaScript”), but now I realize that you’re asking how you ensure that code runs after alltags have been executed when using
coffee-script.js.One solution is to put a callback in the last CoffeeScript you load. Since 1.1.0,
coffee-script.jshas ensured that all CoffeeScriptscripttags are run in order (though note that they’ll only run after all JavaScriptscripttags have run). So, for instance, if you putafter all your other
scripttags, and definewindow.onReadysomewhere else, then that function will be called after all scripts have loaded. If you made it(with jQuery), then you’d ensure that the DOM is ready as well as all scripts.
Update: I posted that this is “one solution” because I wasn’t 100% sure whether there’s a callback that
coffee-script.jsinvokes after all scripts have been run. But after checking the source, I can confidently say that it doesn’t, so the only solution is to put code in the last<script type="text/coffeescript">tag. (Whether it’s inline or in a.coffeefile doesn’t matter.)