I have a list view, with each record (about 50 total) displaying a
priority. I want to let the user click quickly on the priority to
change it (to one of four other variables), with ajax doing the priority
update so the page doesn’t have to repaint. Let’s accept that I hate
dropdowns and say I’m going to display clickable
H M L ?
on each record.
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.
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?
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 (Google Code Archive - Long-term storage for Google Code Project Hosting.) 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 NWEvents Event Manager - Delegation TEST 01
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.