Rjs woes

i’ve just installed the RJS plugin per cody’s instructiuons and read
through
tutorials. I’ve made the necessary changes to my existing code and
everything appears to be working, except that the returned javascript is
not
being evaluated…

here’s my setup:

i have a checkbox with an onclick event pointing to a javascript
function.
this javascript function does a few things then sends a request. I have
the
onSuccess callback pointing to another function who’s only purpose is to
eval(resp.responseText). but this is not happening. i added an alert(
resp.responseText) and the javascript code is there, it’s just not being
evaluated.

checkbox:

onclick="activate_user(this)"/>

javascript:

function activate_user(cb) {
var status = (cb.checked) ? “1” : “0”
var data = “id” + cb.value + “&af=” + status
var myReq = new Ajax.Request( ‘/admin/users/activate’, { :method:
‘post’,
parameters: data, onSuccess: success_handler, …)
}

function success_handler(resp) {
alert(resp.responseText);
eval(resp.responseText);
}

rjs template:

page.visual_effect :highlight, “cb_div”, { :start_color => “#aaffaa” }

the success_handler is getting called, as the alert shows the javascript
being returned as:

new Effect.Hilight(‘cb_div’, {startcolor:#aaffaa});

but as i said, it’s not getting evaluated in the eval() call. (i’m not
seeing the effect)

anyone have any ideas?

Chris

Chris,

There are a few problems here.

page.visual_effect :highlight, “cb_div”, { :start_color => “#aaffaa” }

The option is actually :startcolor and it looks like you’ll need extra
quotes on the hex value, like “‘#aaffaa’” because the
scriptaculous_helper doesn’t correctly place the :startcolor option
within quotes.

the success_handler is getting called, as the alert shows the javascript
being returned as:

new Effect.Hilight(‘cb_div’, {startcolor:#aaffaa});
Is this a typo or did you incorrectly spell Highlight here?

With the double quoting this will become:
new Effect.Highlight(‘cb_div’, {startcolor:‘#aaffaa’});

I tried this out in a quick test and without the extra quotes the
effect wasn’t applied. I got a nice green highlight with the extra
quotes.

but as i said, it’s not getting evaluated in the eval() call. (i’m not
seeing the effect)

You shouldn’t actually have to eval this yourself. Prototype will
eval the response automatically because RJS sets the Content-Type
header to text/javascript.

Hope this helps.


Cody F.
http://www.codyfauser.com

Cody,

thanks so much for the assist. the missing quotes was the issue.

and thanks for the plugin!

Chris