Rails and Javascript - don't mess with the client

To Michael G: if what you mean by autocompleter is something like a date field that completes automatically, then here's your example. If you are talking about something more complex like a field that offers a list of suggested completions that is downloaded from the server via AJAX, I think you will still get the idea which is the same.

Your HTML might be <input id="date" type="text" value="Enter date here..." />

Then you could add some behaviour unobtrusively like this (providing you use prototype and it's Event object, but the idea is independent of it):

var input = document.getElementById("date");

Event.observe(input, 'focus', function() {   if (this.value == "Enter date here...") this.value = ""; });

Event.observe(input,'change',function() {   // some code to check that this.value is a valid date and autocomplete it });

Event.observe(input, 'blur', function() {   if (this.value == "") this.value = "Enter date here..."; });

I hope this is enough of an example to give you an idea. You can also go one step further and use Dan Webb's behaviour code in lowpro. It allows you to attach a behaviour to a CSS class rather than to an individual id as above.

That way you could attach a behaviour to a CSS-class 'date' and then add as many <input type="text" class="date"/> as you wanted to your page and they would all expose the same behaviour without any extra JS.

To Michael S:

Have these specific objects/classes be initialized/instantiated appropriately. How? Two possibilities: Arrange it so that only the relevant files are loaded per controller or write config information in a script block in the header.

Possibility one is understood. Can you give an example for possibility two, I am not sure I understand. How would you write that config information so that only those objects/classes that are needed are loaded from the server. Thanks.

To Phillip: Thanks for the explanation. As far as implementation is concerned, I was thinking that it should be fairly easy. I am not fluent enough in RoR yet to provide a piece of code, but it should be possible in the layout to get the name of the controller class? Then I would just use your snippet and add an if-block that checks if there is a js-file by the same name in /public/javascripts and if there is, include it. If not, nothing. That keeps you flexible, no need to create a js-file for each controller, but if you want to you can, without having to add extra code.

I guess, it should also be possible to do the whole thing using .js.erb instead of plain .js, so if you like you can use the same structure and still embedd ruby into your javascript. Which just brings me to a new idea: You could then have a ruby function like "include_js" that you can call from your .js.erb and that allows you to include further js-files (the function just adds them to your global array that is then rendered in the layout just as you do). I think I might like that, because that way javascript files are included where they are used. I have always thought that it's not very pretty to have to include your JS (be it libraries or just some of your own objects you're making use of...) inside the HTML file whilst they're being used inside other JS files. And as far as I know, unlike CSS, JS doesn't allow any includes or imports, so in that case .js.erb would really provide an extra feature, even for those like me who like their plain JS.

Stefan