I think this is quite basic. If I want to run some RJS effects on an element, but I''m not sure that the element will be there or not, how do I stop an error occurring? e.g. page[''test''].visual_effect :highlight gives an error alert if there is no element with the id of ''test''. I''ve got it to work by using: page[''test''].visual_effect :highlight unless page[''test''].empty? but that doesn''t seem the best way to do it. What is the recommended way to do this? thanks, DAZ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 11 Jun 2008, at 18:38, DAZ wrote:> > I think this is quite basic. > > If I want to run some RJS effects on an element, but I''m not sure that > the element will be there or not, how do I stop an error occurring? > > e.g. > page[''test''].visual_effect :highlight > > gives an error alert if there is no element with the id of ''test''. > > I''ve got it to work by using: > > page[''test''].visual_effect :highlight unless page[''test''].empty? >that doesn''t do what you think it does (take a look at the javascript that is generated) I wrote about this a few weeks ago: http://www.spacevatican.org/2008/5/26/conditional-rjs-explained 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 http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi DAZ, DAZ wrote:> If I want to run some RJS effects on an element, > but I''m not sure that the element will be there or > not, how do I stop an error occurring? > > e.g. > page[''test''].visual_effect :highlight > > gives an error alert if there is no element with the id of ''test''. > > I''ve got it to work by using: > > page[''test''].visual_effect :highlight unless page[''test''].empty? > > but that doesn''t seem the best way to do it. What is > the recommended way to do this?I don''t know about the ''recommended'' part (your approach doen''t seem ''flawed'' to me), but RJS let''s you test for the presence of a DOM element using CSS-based selectors. Assuming you''ve got an element with an id=''test'', you might try... page.select "#test" page[''test''].visual_effect :highlight end HTH, Bill --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 11 Jun 2008, at 19:19, Bill Walton wrote:> > Hi DAZ, > > DAZ wrote: > >> If I want to run some RJS effects on an element, >> but I''m not sure that the element will be there or >> not, how do I stop an error occurring? >> >> e.g. >> page[''test''].visual_effect :highlight >> >> gives an error alert if there is no element with the id of ''test''. >> >> I''ve got it to work by using: >> >> page[''test''].visual_effect :highlight unless page[''test''].empty? >> >> but that doesn''t seem the best way to do it. What is >> the recommended way to do this? > > I don''t know about the ''recommended'' part (your approach doen''t seem > ''flawed'' to me), but RJS let''s you test for the presence of a DOM > element > using CSS-based selectors. > > Assuming you''ve got an element with an id=''test'', you might try... > page.select "#test" > page[''test''].visual_effect :highlight > endThat won''t do much, but page.select("#test").each {|element| element.visual_effect :highlight} should do the trick Fred> > > HTH, > Bill > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Jun 11, 7:19 pm, "Bill Walton" <bill.wal...-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote:> Hi DAZ,> I don''t know about the ''recommended'' part (your approach doen''t seem > ''flawed'' to me), but RJS let''s you test for the presence of a DOM element > using CSS-based selectors. > > Assuming you''ve got an element with an id=''test'', you might try... > page.select "#test" > page[''test''].visual_effect :highlight > endSigh. that will work (and is basically the same as what I wrote) as long as you add the ''do'' that I''m sure you meant to write. Fred> > HTH, > Bill--~--~---------~--~----~------------~-------~--~----~ 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 http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Fred, Frederick Cheung wrote:> > Assuming you''ve got an element with an id=''test'', > > you might try... > > page.select "#test" > > page[''test''].visual_effect :highlight > > end > > That won''t do much, butOops ;-p You''re right. That should have been... if page.select "#test"> page.select("#test").each {|element| element.visual_effect :highlight} > > should do the trickYep. OTOH, I prefer the ''if'' since, IMHO, it reinforces the requirement that DOM ids are unique, whereas it seems to me that the select might not. Best regards, Bill --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> > Oops ;-p You''re right. That should have been... > if page.select "#test" > > > page.select("#test").each {|element| element.visual_effect :highlight} > > > should do the trick > > Yep. OTOH, I prefer the ''if'' since, IMHO, it reinforces the requirementThe if doesn''t work though (since that''s a ruby if, ie a server side if). if you want to use an if then you need a javascript one, ie page << "if($(''foo'')){" ... page << "}"> that DOM ids are unique, whereas it seems to me that the select might not. >the select should be unique, it''s a bad idea to have multiple dom elements with the same id. 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 http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for the feedback on this guys. It''s all been very useful to know. It seems like this is quite a common occurrence (the need to check if an element exists before doing something to it) - it seems strange that an RJS helper doesn''t exist for it - resorting to feeding in JS using page << seems to bit hackish.... DAZ On Jun 11, 9:10 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Oops ;-p You''re right. That should have been... > > if page.select "#test" > > > > page.select("#test").each {|element| element.visual_effect :highlight} > > > > should do the trick > > > Yep. OTOH, I prefer the ''if'' since, IMHO, it reinforces the requirement > > The if doesn''t work though (since that''s a ruby if, ie a server side > if). if you want to use an if then you need a javascript one, ie > > page << "if($(''foo'')){" > ... > page << "}" > > > that DOM ids are unique, whereas it seems to me that the select might not. > > the select should be unique, it''s a bad idea to have multiple dom > elements with the same id. > > 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 http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
ARG. That''s ugly. My bad. Best regards, Bill ----- Original Message ----- From: "Frederick Cheung" <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> To: "Ruby on Rails: Talk" <rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> Sent: Wednesday, June 11, 2008 3:10 PM Subject: [Rails] Re: Some RJS Help> > Oops ;-p You''re right. That should have been... > if page.select "#test" > > > page.select("#test").each {|element| element.visual_effect :highlight} > > > should do the trick > > Yep. OTOH, I prefer the ''if'' since, IMHO, it reinforces the requirementThe if doesn''t work though (since that''s a ruby if, ie a server side if). if you want to use an if then you need a javascript one, ie page << "if($(''foo'')){" ... page << "}"> that DOM ids are unique, whereas it seems to me that the select might not. >the select should be unique, it''s a bad idea to have multiple dom elements with the same id. 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 http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi DAZ, DAZ wrote:> > It seems like this is quite a common occurrence (the need to check if > an element exists before doing something to it) - it seems strange > that an RJS helper doesn''t exist for it - resorting to feeding in JS > using page << seems to bit hackish....An RJS helper does exist. The ''select''. Thanks to Fred, we now know it just needs to be used as he recommends: in a block rather than an ''if then else'' His statement re: using page << wasn''t a recommendation; just a ''if that''s how you what to do it, this is the syntax you''ll have to use''. I''d use his earlier (and better tested than mine) recommend solution. Best regards, Bill --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Jun 11, 10:16 pm, DAZ <daz4...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks for the feedback on this guys. It''s all been very useful to > know. > It seems like this is quite a common occurrence (the need to check if > an element exists before doing something to it) - it seems strange > that an RJS helper doesn''t exist for it - resorting to feeding in JS > using page << seems to bit hackish.... >if you read the blog post I linked to there is a plugin with helpers for this. At a basic level I''d say large amounts of complicated javascript in rjs isn''t really a great idea. Fred> DAZ > > On Jun 11, 9:10 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > > Oops ;-p You''re right. That should have been... > > > if page.select "#test" > > > > > page.select("#test").each {|element| element.visual_effect :highlight} > > > > > should do the trick > > > > Yep. OTOH, I prefer the ''if'' since, IMHO, it reinforces the requirement > > > The if doesn''t work though (since that''s a ruby if, ie a server side > > if). if you want to use an if then you need a javascript one, ie > > > page << "if($(''foo'')){" > > ... > > page << "}" > > > > that DOM ids are unique, whereas it seems to me that the select might not. > > > the select should be unique, it''s a bad idea to have multiple dom > > elements with the same id. > > > 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 http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---