Since Moodle 1.7, we have had a function called require_js, which made it very easy to make sure that you JavaScript files you wanted got linked to somewhere, and that each file was linked to no more than once.
That was very nice, but only went so far. For example, there was no equivalent require_css, and some of the YUI libraries need their own CSS files to work properly. Also, you could not control when the JS was linked to, and as discussed here, it is best to link to most of the JavaScript from the end of the HTML, not from <head> because that gives better page-load performance. Except, of course, some things cannot wait that long, so you need the option to do it sooner. And then, if your .js files are only linked to at the end of the document, but you need to call some function to initialise things, then that function call had better be at the end too.
So the requirements get a bit more complicated, and so to handle them, I have made a new class called page_requirements_manager to replace require_js. You use it like this:
$PAGE->requires->css('mod/mymod/styles.css'); $PAGE->requires->yui_lib('autocomplete'); // (1) $PAGE->requires->js('mod/mymod/script.js'); $PAGE->requires->js('mod/mymod/small_but_urgent.js')->in_head(); $PAGE->requires->js_function_call('init_mymod', array($data))->on_dom_ready();(1) Note that yui_lib knows that the YUI autocomplete library requires the other libraries 'yahoo-dom-event', 'datasource' and also 'autocomplete/assets/skins/sam/autocomplete.css'
Anyway, the code for page_requirements_manager is done, and currently attached to MDL-16695. If anyone would like to review it before I commit it tomorrow, that would be great. (I already had a lot of excellent feedback from Penny.) There are full PHPdoc comments and unit tests, which I hope make it easier to understand.
Note, I have not yet made a new, deprecated version of require_js for backwards compatibility; nor have I hooked this new class into print_header; nor changed all the places that currently call require_js. I will, but I was waiting in case people didn't like what I had done in page_requirements_manager.
And, if you are doing JavaScript-y thing in HEAD for Moodle 2.0, please be aware that this new API is coming along. I hope it makes you life easier.