Simple Rendering Question

I have a simple eRb snippet that get’s a hash via an objects method
call,
and populates a Javascript call… for example:

<%= @hash = asdf.getHash() %>

When I view the source of the resulting HTML, the doSomething() method
is
indeed populated with the correct value from the hash… however, the
actual
method get’s called “without” the value.

So, I’m assuming that there is some reason the Javascript is being
rendered
and executed prior to the eRb code.
Is this correct ?

Any insight into this would be appreciated !

Dylan S. wrote:

<%= @hash = asdf.getHash() %>

When I view the source of the resulting HTML, the doSomething() method
is indeed populated with the correct value from the hash… however, the
actual method get’s called “without” the value.

Should the hash value be quoted?

blah.doSomething( '<%= @hash['blah'] -%>' );

I’m actually extracting the values from the hash above the script… so:

Get the Hash from the Method

<% @location_hash = @listing.getData %>

Assign “@stuff” the value of the stuff value in the Hash

<% @stuff = @location_hash[‘stuff’] %>

Use “@stuff” in the Javascript

new RandomJavascriptObject(<%= @stuff %>);

The catch is, if I hardcode the value of @stuff in the actual HTML, and
load
the page… everything works fine.
If I use eRb to do the printing like above, the value is actually
printed in
the HTML, but the actual value passed to the Javascript is empty.

The parameter for the Javascript Object is a number. I tried doing @
stuff.to_i and @stuff.to_s, but no luck.

Dylan S. wrote:

Use “@stuff” in the Javascript

new RandomJavascriptObject(<%= @stuff %>);

The catch is, if I hardcode the value of @stuff in the actual HTML, and
load the page… everything works fine.
If I use eRb to do the printing like above, the value is actually
printed in the HTML, but the actual value passed to the Javascript is empty.

What type of JavaScript object is the parameter? Number, string, or
other?

I just tried doing a custom “render”, and also put the Javascript call
with
hardcoded values in a file to “render_partial”… but no luck.
WTF.

Anybody have any ideas ? Just trying to pass a parameter into a
Javascript
method. Simple stuff, but it seems impossible !

Hmm… no luck. I’m baffled :slight_smile:

The HTML source has the function and the parameters on the same line:
new BlahObject(1234, 1234);

… and if I hardcode the parameters to replicate the above, everything
works out perfectly.

So, I’d have to assume that this is a “Javascript loading before eRb”
issue… or something similar.
I’ll keep trying different options to see if I can’t nail this down.
Thanks for you help thus far !

Dylan

Dylan S. wrote:

Anybody have any ideas ? Just trying to pass a parameter into a
Javascript method. Simple stuff, but it seems impossible !

Perhaps your web server is delivering the function name
and parameters in different response chunks because of
possible long delays in erb output, and your web browser
is not waiting for all the chunks before evaluating the
script section. Try generating the whole script section in erb:

<%= javascript_tag “new RandomJavascriptObject( #{@stuff} );” -%>

it will work if you wait until the page has loaded before calling the
javascript function:

it works for me, but not, as you have pointed out, when you try to call
javascript
before the page has finished loading.

script/about
About your application’s environment
Ruby version 1.8.3 (i386-cygwin)
RubyGems version 0.8.11
Rails version 0.14.3
Active Record version 1.13.0
Action Pack version 1.11.0
Action Web Service version 0.9.3
Action Mailer version 1.1.3
Active Support version 1.2.3

I have noticed that for some reason beyond my understanding javascript
functions work
more consistently when placed either in the HEAD or “included” within
the HEAD (not w/in the BODY).
And some of the most common javascript errors are caused by race
conditions (e.g. page loading),
which is why I always wait for the page to load before calling any
javascript.

-lv

Dylan S. wrote:

The parameter for the Javascript Object is a number. I tried doing
@stuff.to_i and @stuff.to_s, but no luck.

Hmm, all I can think of trying is to change the closing tag
from “%>” to “-%>” to keep the closing parenthesis of the
function call on the same line.