Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RJS or Javascript?

I've used RJS in the past for RoR projects and felt terribly constrained by what it could do. However, using Javascript alone felt/feels ugly and hack-y. This is particularly true when writing Javascript that manipulates Rails automagically generated from variable names. I haven't seen much talk about RJS in the blogosphere recently. Is RJS being used in new RoR projects or have people decided that it's not effective? Is it still being actively developed and its function-coverage expanded? I'd appreciate some insight into the current state of affairs.

So, who's using RJS (and how is it working out for you) and who's using javascript?

like image 954
Ron Gejman Avatar asked Dec 20 '09 04:12

Ron Gejman


3 Answers

I recommend writing straight javascript. I believe that - yes - RJS is going out of style. One reason for this is the popularity of the sexy jQuery library. Another is the model of RJS - in that it is a ruby wrapper around javascript, and so for any javascript library you need to use, you will need a corresponding ruby wrapper library, which means more grunt work somebody's going to have to do(and another gem you'll have to depend on). Also, although the idea of making a request and receiving back executable javascript is nice, I believe there are many who don't like this style, or at least don't think it's appropriate for certain situations. I personally have learned javascript and have come to like it a lot, and I recommend you give it a try.

like image 105
airportyh Avatar answered Nov 18 '22 17:11

airportyh


I like using RJS for simple tasks, like:

page["post_#{@post.id}"].replace :partial => @post
page["post_#{@post.id}"].highlight

Yes, you could do this directly with the link_to_remote function, but that just clutters up your view with code, or with an update_page in the controller, but that is ugly.. the rjs allows you to write more clearly understandable code that's uncoupled from any javascript library (since there are things like jrails out there, or you can just override the Rails' methods yourself.)

If you have a really complex javascript function for your application, you'd probably be better served by writing the javascript yourself, since at that point, you won't want to rely on the abstractions that Rails provides.

like image 30
Dan McNevin Avatar answered Nov 18 '22 18:11

Dan McNevin


By RJS, I assume you are referring to RJS templates. The whole concept is that you are generating JavaScript that will be run in a JS eval function in the browser as an AJAX return. How exactly is it that you "felt terribly constrained by what it could do"? You can mix Ruby and JS in the RJS files in a variety of ways, and it isn't any more constraining than other ERB type formats. It's a very powerful way to make AJAX calls do more than update a single <div> (they can even update two <div>s).

I have a feeling you are really meaning to ask about using the JavaScript/Prototype/Scriptaculous Helpers. Is that so?

like image 2
MattMcKnight Avatar answered Nov 18 '22 18:11

MattMcKnight