I have some RJS that could use some refactoring. I use the function called with afterFinish twice and Im wondering what the best way to refactor is. This is all inline rjs in the controller mind you: page.visual_effect :slide_down, "add_appt_#{params [:day]}", :afterFinish => "function(){ $(''end_drop_#{params[:day]}_#{params [:appt_counter]}'').style.display = ''block''; $(''end_drop_#{params[:day]}_#{params [: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''; }" --~--~---------~--~----~------------~-------~--~----~ 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 groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 11 Feb 2009, at 10:41, David wrote:> > I have some RJS that could use some refactoring. I use the function > called with afterFinish twice and Im wondering what the best way to > refactor is. This is all inline rjs in the controller mind you: > > page.visual_effect :slide_down, "add_appt_#{params > [:day]}", :afterFinish => "function(){ > $(''end_drop_#{params[:day]}_#{params > [:appt_counter]}'').style.display = ''block''; > $(''end_drop_#{params[:day]}_#{params > [: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> > >--~--~---------~--~----~------------~-------~--~----~ 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 groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 11 Feb 2009, at 10:41, David wrote: > > > > > > > I have some RJS that could use some refactoring. I use the function > > called with afterFinish twice and Im wondering what the best way to > > refactor is. This is all inline rjs in the controller mind you: > > > page.visual_effect :slide_down, "add_appt_#{params > > [:day]}", :afterFinish => "function(){ > > $(''end_drop_#{params[:day]}_#{params > > [:appt_counter]}'').style.display = ''block''; > > $(''end_drop_#{params[:day]}_#{params > > [: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 > >--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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); }); }> Thanks for your help, Dave > > On Feb 11, 4:17 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> On 11 Feb 2009, at 10:41, David wrote: >> >> >> >> >> >>> I have some RJS that could use some refactoring. I use the function >>> called with afterFinish twice and Im wondering what the best way to >>> refactor is. This is all inline rjs in the controller mind you: >> >>> page.visual_effect :slide_down, "add_appt_#{params >>> [:day]}", :afterFinish => "function(){ >>> $(''end_drop_#{params[:day]}_#{params >>> [:appt_counter]}'').style.display = ''block''; >>> $(''end_drop_#{params[:day]}_#{params >>> [: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 >> >> > >--~--~---------~--~----~------------~-------~--~----~ 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 groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---