- Some larger libraries and plugins only need to appear on single pages (e.g. Thickbox, FCKEditor).
- Some Ajax page renders depend on core libraries (e.g. jQuery) but shouldn’t be loaded more than once by layouts and partials
Oh what a tangled web we weave.
The solution I arrived at is to specify sectional order of libraries, accumulate them, deduplicate them, and include them at the end of the page request. This allows definition of core libraries at the top of a layout page for all partials and inclusion of librararies in plugins on any view or partial without duplicate loading or core library omissions. To do this I defined two Application Helpers as follows:
accumulate_js(:core, ["jquery-1.3.2.min.js", "jquery-ui-1.7.1.custom.min.js"]) accumulate_js(:plugins, "beautytips/jquery.bt.min.js") accumulate_js(:application, "application")
For the specifics of what it does, here’s the rspec spec:
Marc Grabanski, excellent jQuery developer that he is, contributed his sage advice to this solution.
I might put this in a plugin and post to github, but just seems a little silly since there are only two helper methods. What think ye? If this blog post is good enough for you, then it’s good enough for me. Obviously, I’m busy enough that I’ve gone bonky and resorted to archaic english – but if you request a plugin or some other container I may be obliged.