开发者

RJS or Javascript?

开发者 https://www.devze.com 2022-12-14 16:17 出处:网络
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 Javasc

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 manipulat开发者_如何学JAVAes 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?


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.


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.


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?


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.


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.

0

精彩评论

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