views:

233

answers:

2

So it looks like on RoR, when Ajax (using form_remote_tag) returns a success code, Javascript is also returned to handle the visual effects. (this is the RJS mechanism)

using Fiddler, I do see the following response:

try {
Element.update("vote_score", "Score 58");
$("vote_score").visualEffect("highlight");
} catch (e) { alert('RJS error:\n\n' + e.toString()); alert('Element.update(\"vote_score\", \"Score 58\");\n$(\"vote_score\").visualEffect(\"highlight\");'); throw e }

Will there be situation where the code is quite big (like 1 or 2k) that RJS can use some sort of static Javascript already loaded to handle the effect?

Is there any other ways to use static Javascript already loaded with the page? Must it be just raw Javascript and use the Prototype's Ajax success response to call the static Javascript code, or can it be some Rails mechanism related to RJS?

A: 

You can - however not using RJS or other RoR helper methods. Send request to server via AJAX and return short JSON or XML response which you parse on client-side instead of loading JavaScript code.

Particulary nice pattern is using unobtrusive javascript - HTML does not mention JS except small part which adds callbacks on load (how to use it in rails 3).

Maciej Piechotka
+1  A: 

You can have your code already in a javascript file or just in the head of the page, and make the RJS call reference it. The code that is returned can execute functions that are already on the page.

The syntax is:

page.call 'function_name', arg1, 'Second arg', 'etc...'
Tilendor