Forum: Ruby on Rails Re: Refactoring RJS

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Frederick C. (Guest)
on 2009-02-11 14:18
(Received via mailing list)
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
David (Guest)
on 2009-02-12 05:00
(Received via mailing list)
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>
Frederick C. (Guest)
on 2009-02-12 13:19
(Received via mailing list)
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);
  });
}
This topic is locked and can not be replied to.