Prototype: Inject JS variable names into generated code?

Now, it seems like adding link_to_remote or form_remote for this on
EVERY record (and maybe every H,M,L, and ?) is just a waste of
bandwidth. Tiny, yeah, but let’s use this example to help define the
approach. I’d think I could define a single javascript function on the
page like upd( num, new_pri ) that would invoke the Ajax updater to call
the priority_update action on the back end. The javascript variables
num and pri would have to be injected into the Ajax call, of course. On
each record, then, I’d only have something with onclick=“upd( 33, 2);”
or similar.

If you’re going to clean up a mess, using global functions isn’t really going to improve matters all too much is it?

This problem is I’m too new to all this Prototype / Rails stuff to make
it work. How do I inject variables like num and new_pri into the code
generated by prototype? Or is this just an incredibly stupid idea and I
should be flogged?

There are a couple of ways to go about it and the best one imho is event delegation, so you don’t have to worry about unbinding and rebinding of new ajax inserted elements and having one event handler on a page is much more efficient than one on every single list element. I’m using NWEvents (http://code.google.com/p/nwevents/)) in conjunction with prototype & scriptaculous (because nwevents has built-in support for form submit and focus/blur, those events don’t bubble). Works like a charm. Example code on http://javascript.nwbox.com/NWEvents/delegates.html

You just use normal link_to in your rails app (if you want both normal and ajax links on your page, add a “remote” class to your link_to) and then tell nwevents to trigger the href of the link to with an Ajax.Request or something.

Rails helpers will do a nice job for all the basic stuff, but once it gets fairly complicated (with callbacks and the likes), you either need to use awful hacks such as page.delay (try and have a visual effect to hide an element and then actually remove the element from the page) or page << “customjscode” everywhere. And yes, all that inline javascript makes the size of the page huge in many cases.

Best regards

Peter De Berdt