I have been a fan of UJS for the longest time and I usually put ALL my code in application.js(jquery, not prototype)
However, I have come across a huge project that will bloat up application.js if left untended and I don't want that to happen, especially since most of my functions/behavior will be based on specific pages. Right now, I am inclined to put the javascript calls on specific partials, like so:
on _test_partial.html.haml:
%p= link_to "Expand All", '#', :class => "expand-categories"
:javascript
$(function() {
$('.expand-categories').click( function() {
$(".test-cases").slideToggle();
return false;
})
})
Or if I really wanted "unobtrusive" stuff:
%p= link_to "Expand All", '#', :class => "expand-categories"
= javascript_include_tag 'test-categories.js'
But it seems to me that this is still very obtrusive, as if I have to edit the code/behavior, I will have to look for the partial concerned. On the other hand, isn't that easier that scouring a 1MB(there are lots of pages and there will probably lots of page specific code) application.js of a function i need to edit.
What's the usual take on this? Is there a way for me to name a js file ala RJS like test_partial.js.erb os something?
I personally try to keep all my javascript under public/javascript as much as possible. However I break my application.js file into multiple files based on "sub-domains", e.g. application.profiles.js, application.albums.js, etc... with each file implementing the Module Pattern. This approach allows you to nicely compartmentalize your javascript code, load it as needed and I believe addresses your concern of being able to quickly find functions.
Given one file or "sub-domain" (e.g. application.profiles.js), you can further compartmentalize your code using the "Augmentation Pattern" described in this article. So you may end up with something like application.profiles.core.js and application.profiles.viewer.js, where for example, the application.profiles.viewer.js module augments the application.profile.core.js module.
Hope this helps.