# Prototype: Inject JS variable names into generated code?

**URL:** <https://www.ruby-forum.com/t/prototype-inject-js-variable-names-into-generated-code/147177>\
**Category:** Rails\
**Created:** [August 29, 2008, 3:47am UTC](https://www.ruby-forum.com/t/prototype-inject-js-variable-names-into-generated-code/147177 "2008-08-29T03:47:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mikec](https://www.ruby-forum.com/u/mikec)\
**Post date:** [August 29, 2008, 3:47am UTC](https://www.ruby-forum.com/t/prototype-inject-js-variable-names-into-generated-code/147177/1 "2008-08-29T03:47:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mikec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mikec](https://www.ruby-forum.com/u/mikec)\
**Post date:** [August 29, 2008, 10:17am UTC](https://www.ruby-forum.com/t/prototype-inject-js-variable-names-into-generated-code/147177/2 "2008-08-29T10:17:47Z")

</div>

On 29 Aug 2008, at 03:47, Mike C. wrote:

> 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.](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  
[NWEvents Event Manager - Delegation TEST 01](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
