views:

219

answers:

5
+3  Q: 

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?

A: 

RJS was never intended to be a flat-out replacement for JavaScript, and you shouldn't decide which tool is right for the job based on what you think you see some other kid doing on the blogosphere.

Azeem.Butt
Disagree. If people aren't interested in RJS it won't continue to be supported, developed, worked with and generally visible. I'd rather not continue to invest time in working with it if people are no longer using it. The RJS community is a subset of the JS community so it's already hard to figure out how to do things in RJS compared to JS. If RJS is *going out of style* then I can imagine that situation getting worse. So yeah, I do want to know what other people are doing and where they're putting their time and energy into.
Ron Gejman
I think what @NSD was tring to say is that you shouldn't choose to use a specific technology just because other people are using it; however, that doesn't preclude *not using it* because other people aren't using it.
Justin Johnson
Right, but I asked in order to find out if people are not using it anymore.
Ron Gejman
+1  A: 

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?

MattMcKnight
I agree with Matt. I think the reason RJS is perceived to be falling by the wayside is that generates JS for the Prototype library unless you use a plugin that overrides the helper methods, and a lot of developers are picking up JQuery. If you use Prototype, disregarding RJS completely is ill-conceived IMO.
Steve Graham
+3  A: 

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.

toby
This is the sort or answer I was looking for. Has there been any explicit talk about RJS's future that I could look at? If not, why do you feel that it's going out of style?I do know JS, which is why I am debating which to use for my next project. I can figure out on technical and ease-of-developing grounds which to use, but I'm concerned about RJS's future altogether.
Ron Gejman
A: 

I personally do not like the js generated by the helpers, so I usually opt to not use them. However, there are times when they are quite convenient.

There is also a Jquery replacement, Jrails, if your not a Prototype fan.

But, I have to agree with NSD. Why would you let what others do ultimately lead your path. If you don't like the way the helpers work or they are not doing it for your project, don't use them. Or if it's missing something, write your own. And perhaps in that, you can ignite some intrest.

As for investing time, it's knowledge.

nowk
+1  A: 

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.

Dan McNevin