开发者

Can Ruby on Rails's RJS give out static Javascript code to handle user interaction?

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 }

W开发者_如何转开发ill 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?


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜