Quick RJS question.

I got a short question about RJS.

Is it possible to get the RJS to "compile" to Javascript code without having to use AJAX? As in, can I use RJS to bundle some javascript code with my page when it's first created?

Thanks, Mathias Wittlock.

You can use all the helpers you use in a RJS template in a vie as well, you just dont need the page obejct. you can also use the JavaScript Helpers to create script tags, links with function calls etc.for this.

link_to_function "hide menu", visual_effect(:fade, "menu")

Hmm, this pointed me in the right direction to get what I wanted. Thanks a bunch, much appreciated! :slight_smile:

Mathias.

Thorsten gave you one answer---probably the one you wanted. Another answer is simply "yes". You can use te JavaScriptGenerator which RJS invokes in any view context (or instantiate it manually if you want to go through those pains). Here's what it looks like:

update_page do |page|   page['an-element'].visual_effect :shake end

This block will actually return the generated Javascript. So, for example, if you wanted to do your own special link_to_shake helper, you could do something like this (untested):

def link_to_shake(element_id)   link_to_function "Shake it", update_page do |page|     page[element_id].visual_effect :shake   end end

Chad

This could be usefull in debugging RJS output for those of us that have no clue about RJS and want to see the javascript it's generating...

If I were writting normal javascript I would do:

$('attribute[<%=index%>][id_to_me]').value = ""; $('filename- <%=index%>').value = "";

but I want to return javascript as an Ajax response so I'm doing:

page.select("#attribute[#{index}][id_to_me]").value = ""; page.select("#filename-#{index}").value = "";

And it's not working, and I'm not really sure how to debug it.

page.select("#attribute[#{index}][id_to_me]").value = ""; page.select("#filename-#{index}").value = "";

you don't use the # at the start of the id's name in RJS, simply because it's not used in Prototype Javascript as well.

page.select("attribute[#{index}][id_to_me]").value = ""; page.select("filename-#{index}").value = "";

The best way is to use FireFox with FireBug. You can inspect the response of an XMLHTTPRequest and see the generated Javascript. Very handy.

Chad