UJS; RJS versus POJS; Prototype Stack versus JQuery Stack

I am accustomed to using Unobtrusive JavaScript (UJS) in my apps. UJS dynamically adds event handlers to the DOM. These event handers implement behaviors via DOM manipulations and XMLHttpRequests (XHR’s). This approach is in contrast to the historical approach of specifying event handlers on HTML elements directly. The value of the UJS approach are:

  • it separates behavior (JavaScript) from markup (HTML) and this is good for the same reasons separating style (CSS) from markup is good
  • it can reduce page weight

I had been using ujs4rails but that thing is being deprecated so it isn’t an option for me. If I want to do UJS I can either use Prototype or JQuery. I don’t know of any other good options.

I’m using JQuery a bit now. Pretty light use: a highlight effect here, a show/hide toggle there; some AJAX form submission. In thinking about how to proceed I’m kind of torn. I don’t want to use both JQuery and Prototype longer term. For starters, that’s too much mental baggage to carry around — one must gain fluency in one or ‘tother I think. Also it’s a lot of page weight to load both libraries.

Another dimension is RJS vs. POJS. RJS is Rails’ templating and API that lets you generate JavaScript from Ruby. POJS is “Plain Old JavaScript”. If we do RJS then we almost have to stay on the Prototype stack. But with POJS a guy could go with either stack and in fact I think the scale tips to the JQuery stack for POJS as I’ll show in a sec.

So here’s how I see the top two options:

RJS: Prototype + Scriptaculous + RJS + Dan Webb’s Lowpro:
+ Lowpro supports UJS
+ most of us have used prototype + scriptaculous and are somewhat comfortable with it
+ native Rails (RJS) support
– Lowpro is not as well documented (nor as widely used) as JQuery for UJS
– I see no centralized library of plugins (other than Scriptaculous itself) for this stack

POJS: JQuery + MinusMOR:
+ it supports UJS out of the box in a pretty clean way
+ people I respect seem to be moving to JQuery
+ there is a large library of plugins including autocompleters and flash upload progress indicators
– very little native Rails (RJS) support — you can do some things but it’s hard to know exactly what will work
– if you already know scriptaculous effects, you have to learn new effects

A big part of this decision hinges on the value of RJS. After using Rails RJS templates for a year and a half or so my conclusion is that they are more trouble than they are worth. Many believe RJS is easier to write/maintain that POJS — especially for a Ruby programmer. I believe RJS is actually a false economy. In real applications you really have to code in real JavaScript.

An RJS template generates JavaScript from Ruby code — but that Ruby API is insufficiently documented (in particular with regard to the scriptaculous effects) and insufficiently capable (e.g. there is no easy way to update DOM elements matching a CSS selector through that API). Essentially a guy can waste a lot of time trying to get RJS to work for anything but the simplest demos. So I lean toward plain-old JavaScript (POJS) instead.

I wonder what you think. Do you place higher value on RJS for real applications? Have you found an alternate stack, or perhaps a different combination of stack elements? Must I pick one stack or the other, or is it possible and profitable to use both at once?

This entry was posted in AJAX, RJS templates, Ruby on Rails, script.aculo.us. Bookmark the permalink.

4 Responses to UJS; RJS versus POJS; Prototype Stack versus JQuery Stack

  1. bryan says:

    I am not a RoR developer so I cant give you any advice about your Ruby predicament. I do, however, love jQuery. You said one thing right – people who I respect are moving to jQuery. I am think for good reason! I am sure the Prototype will eventually emulate some of the things jQuery is doing to make life easier for us. It is just how the game works – if a product is doing better than yours, you figure out why and you make your product do the same – or do it significantly better. I dont know if Prototype can do it better. Personally, I have already made the switch to jQuery about two months ago and it has changed the way I code. Everything is prettier and makes more sense to read.

  2. jonah says:

    jQuery is definitely the way to go for me. RJS seems to be simply a stepping stone for programmers that are scared of javascript (i.e. they dont know it very well).

  3. louis says:

    i am a rails developer, and i’m not a fan of rjs. in fact, i started w/ ruby, then rails, then got tired of rjs and a lot of the mixing of xhtml, css, and javascript that is the out-of-the-box rails experience. i appreciate the ‘shield rails programmers from any non-ruby/rails code’, and they’ve done a fantastic job w/ most of it. but, i just went ahead and learned js and use that exclusively. i will at minimum, use jquery for it’s document.ready() and a lot of the features that libraries excel at: cross-browser issues. and at maximum, i love the chaining. 1 line to take care of what 10 lines of pojs does is hard to argue against.

    plus, jquery.min is *tiny* compared to the :defaults that rails wants you to use. and you can choose to load only the plugins you want on a per-site basis.

  4. There are certainly other options for unobtrusive JS. I’ve done a complex Ajax application with Mootools, unobtrusive-style coding, and PHP.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s