# Re: Refactoring RJS

**URL:** <https://www.ruby-forum.com/t/re-refactoring-rjs/160066>\
**Category:** Rails\
**Created:** [February 11, 2009, 1:18pm UTC](https://www.ruby-forum.com/t/re-refactoring-rjs/160066 "2009-02-11T13:18:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fcheung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/90db22/32.png) [@fcheung](https://www.ruby-forum.com/u/fcheung)\
**Post date:** [February 11, 2009, 1:18pm UTC](https://www.ruby-forum.com/t/re-refactoring-rjs/160066/1 "2009-02-11T13:18:25Z")

</div>

On 11 Feb 2009, at 10:41, David wrote:

> [:appt\_counter]}’).style.visibility = ‘hidden’;  
> $(‘start\_drop\_#{params[:day]}_#{params  
> [:appt\_counter]}’).style.display = ‘block’;  
> $('start\_drop_#{params[:day]}\_#{params  
> [:appt\_counter]}’).style.visibility = ‘hidden’;  
> }"

Yummy :-). if you’re using prototype you can replace

$(‘end\_drop\_foo’).style.display = ‘block’;  
$(‘end\_drop\_foo’).style.visibility = ‘hidden’;

with

$(‘end\_drop\_foo’).setStyle({display: ‘block’, visibility: ‘hidden’})

If i were you I’d have in my application.js a function like

function resetVisibility(element){  
element.setStyle({display: ‘block’, visibility: ‘hidden’})  
}

then then your rjs stuff looks like

resetVisibility(‘end\_drop\_#{params[:day]}_#{params[:appt\_counter]}’)  
resetVisibility('start\_drop_#{params[:day]}\_#{params[:appt\_counter]}’)

or even right the whole thing as a js function (RJS can be a bit of a  
crutch)

Fred

---

<div class="post-metadata">

**Author:** ![fcheung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/90db22/32.png) [@fcheung](https://www.ruby-forum.com/u/fcheung)\
**Post date:** [February 12, 2009, 4:00am UTC](https://www.ruby-forum.com/t/re-refactoring-rjs/160066/2 "2009-02-12T04:00:49Z")

</div>

Hi Fred, thanks so much for your response, that is exactly what I was  
looking for. What do you mean by rjs can be a bit of a crutch? What  
do you mean by rewriting the whole thing as a js function. You mean  
just something like resetVisibility() and then just reset both the  
end\_drop and start\_drop elements within the resetVisibility function?

Thanks for your help, Dave

On Feb 11, 4:17 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![fcheung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/90db22/32.png) [@fcheung](https://www.ruby-forum.com/u/fcheung)\
**Post date:** [February 12, 2009, 12:19pm UTC](https://www.ruby-forum.com/t/re-refactoring-rjs/160066/3 "2009-02-12T12:19:33Z")

</div>

On 12 Feb 2009, at 02:59, David wrote:

> Hi Fred, thanks so much for your response, that is exactly what I was  
> looking for. What do you mean by rjs can be a bit of a crutch?  
> I mean that you can torture yourself for a long time trying to  
> convince rjs to do something whereas you could get it done quicker  
> (and probably end up with nicer results) just be writing the JS. Sure  
> you’ll need to spend a bit of time getting familiar with javascript  
> and prototype/jquery/whatever library you end up using but it will be  
> worth it in the long run

> What  
> do you mean by rewriting the whole thing as a js function. You mean  
> just something like resetVisibility() and then just reset both the  
> end\_drop and start\_drop elements within the resetVisibility function?

replace the whole rjs block by a call to a function that looked  
something like

function do\_it(element\_to\_slide, end\_drop\_element, start\_drop\_element){  
new Effect.SlideDown(element\_to\_slide, {  
afterFinish: function(){  
resetVisibility(end\_drop\_element);  
resetVisibility(start\_drop\_element);  
});  
}
