Fleegix.org

RJS in reverse -- JRS?

2006-11-25 07:44:00

Now this makes a bit more sense to me -- Dan Webb's MinusR/MinusMOR (some other good commentary on Luke Redpath's blog). Rather than coding in server-side Ruby and trying to convert that to client-side JavaScript, how about coding in JavaScript that allows embedded Ruby code in it?

His comment about using server-side code in one language to produce client-side code in another is spot-on:

When RJS first came out I was quite impressed with the Ruby to JavaScript translation stuff but it’s limitations (and that of lots of other similar attempts at 'compiling down to JavaScript' like Google Web Toolkit) shine through after the lightest of uses.

With RJS (and other similar approaches), if you want to do something complicated, it's easy to end up with a Frankenstein-monster approach with lots of heredocs or concatenated strings of raw JavaScript.

This reverse-RJS turns that notion neatly on it's head. Your target is code that runs in the client-side environment -- why not start in there -- in JavaScript -- and allow islands of the server side language wherever you need it?

Then you end up with pretty reasonable looking code like this:

if (someClientSideVariable) {
    $('a').update(<%= js render(:partial => 'thing') %>);
} else {
    $('b').update(<%= js render(:partial => 'thong') %>);
}

I'm sure it depends on how complex your client-side code is, and how comfortable you are with JavaScript -- but this seems like a nice compromise that would you all the flexibility you'd get from writing pure client-side code, but a lot of extra power to tap the tasty Rails goodness going on server-side.

About

This is the blog for Matthew Eernisse. I currently work at Yammer as a developer, working mostly with JavaScript. All opinions expressed here are my own, not my employer's.

Related

Previous posts

All previous posts »

This blog is a GeddyJS application.