Monday, November 27th, 2006

Minus(MO)R a.k.a. Let me write JavaScript but help me!

Category: JavaScript, Library, Rails

Dan Webb has created two Rails plugins to make RJS even better. He has realised that although for some cases RJS is a great thing, you often do want to just use JavaScript, and it looks really ugly to do this:

  1. page << 'if (someClientSideVariable) {'
  2. page['a'].replace_html :partial => 'thing'
  3. page << '} else {'
  4. page['b'].replace_html :partial => 'thong'
  5. page << '}'

He first came out with MinusR which takes of your rjs files and lets you code in JS, but still call out to the ruby style. For example:

javascript

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

MinusR gives you the js helper that calls to_json on any value passed to it so you can drop nicely escaped and formatted data into your script as before. Everyone’s a winner. Well, not quite, I could be going out on a limb here but I really think this is the better way. Go on, give it a go. A bit of javaScript won’t hurt you….

Then a bunch of people wanted this to be doable, but in a separate file, so Dan created MinusMOR, which let’s you do the same thing in .ejs files.

It is great to be able to choice your poison, even on a case by case basis, and decide whether JS or Ruby is the ruler.

Posted by Dion Almaer at 8:03 am
1 Comment

+++--
3.3 rating from 14 votes

1 Comment »

Comments feed TrackBack URI

What looks ugliest by far in both examples is the overabundance of HTML quoting. I’d assume that’s an ajaxian problem though, since the improved version is just as plagued as the non-improved one.

Comment by Johan Sundström — November 30, 2006

Leave a comment

You must be logged in to post a comment.