Some RJS Help

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

On 11 Jun 2008, at 18:38, DAZ wrote:

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:

Fred

Hi DAZ,

DAZ wrote:

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

On 11 Jun 2008, at 19:19, Bill W. wrote:

page[‘test’].visual_effect :highlight
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

That won’t do much, but
page.select("#test").each {|element| element.visual_effect :highlight}

should do the trick
Fred

Hi Fred,

Frederick C. 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, but

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
that DOM ids are unique, whereas it seems to me that the select might
not.

Best regards,
Bill

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

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 C. [email protected]

On Jun 11, 7:19 pm, “Bill W.” [email protected] 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
end

Sigh. 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

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

ARG. That’s ugly. My bad.

Best regards,
Bill

----- Original Message -----
From: “Frederick C.” [email protected]
To: “Ruby on Rails: Talk” [email protected]
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 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

On Jun 11, 10:16 pm, DAZ [email protected] 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