SD Ruby,

I'm adding a bit of JS to a Rails 3.1 app.  Specifically, the JS I
wrote using jQuery disables a few controls in a form if they're not
applicable to the user's situation.

As an example, I've got two radio buttons that let you choose one of
two methods for adding a name to a list.  If you have some pre-defined
contacts you can select one of them, but if you don't, you can add the
person's name and email as a contact.

When the page is loaded the user's existing contacts are loaded into
the @contacts class variable and the select_tag uses that collection
to populate itself.  If the collection is empty I disable the select
and the radio button associated with it and select the radio button
for entering the name and email directly and enable the inputs.

My question is, what's the best practice for loading this type of JS
into the page?  The way I currently do it the functions.js file is
loaded via the asset pipeline, and jQuery attaches my code to the
proper DOM elements when the page is fully loaded.

There are three groups of controls that need this treatment on the
same page, and it seems awkward to me to be making reference to three
specific ID and class selectors that only appear on one page in JS
code that's loaded on every page in the app.

It seems like I should instead be specifically loading this code on
just the page that needs it, but using a script tag on that page
doesn't seem terribly unobtrusive, either.  My Google-fu is letting me
down on this one.

I know we have some serious JS gurus in our midst.  Can one of you
guide this young Jedi?

Thanks,

Chris

-- 
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby

Reply via email to