Monday, November 27th, 2006
Minus(MO)R a.k.a. Let me write JavaScript but help me!
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:
-
page <<'if (someClientSideVariable) {'
page['a'].replace_html :partial => 'thing'
page <<'} else {'
page['b'].replace_html :partial => 'thong'
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:
-
-
if (someClientSideVariable) {
-
$('a').update(<%=js render(:partial => 'thing') %>);
-
} else {
-
$('b').update(<%=js render(:partial => 'thong') %>);
-
}
-
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.












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.