Mike Cahill
2008-Aug-29 01:47 UTC
Prototype: Inject JS variable names into generated code?
There''s probably a smarter way to do this. 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? Thanks - M -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Peter De Berdt
2008-Aug-29 08:16 UTC
Re: Prototype: Inject JS variable names into generated code?
On 29 Aug 2008, at 03:47, Mike Cahill wrote:> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---