Unobtrusive Javascript in Rails...what if?

What if...you could produce accessible, unobtrusive javascript, using Rails built-in javascript/prototype helpers, with just one extra line of code in your layout, a plugin, and one small enhancement to the helpers? Something like this:

<% form_remote_tag :controller => 'foo', :action => 'bar', :unobtrusive => true %>

Which produces:

<form id="form_foo_bar" action="/foo/bar" method="post">

But which still acts as an ajax form? You would simply handle the response for ajax and non ajax requests using Rails' responds_to function.

In addition, what if you could attach javascript functionality to your page elements in a Behaviour-like fashion, but using pure Ruby, anywhere in your view, but loaded from an external Javascript file?

Sorry to tease. Stay tuned, we have something very cool to show off in the next day or two.

http://opensource.agileevolved.com

Return to home page | Check out my tumblelog

4 Comments on this article

1. Comment by Arto Bendiken on 06 Jun 2006 at 08:06

That looks very interesting, and would go a long way to sate the vocal minority who object to all the inline JavaScript produced by Rails. Hopefully you could submit this as a patch to Rails?

2. Comment by Luke on 06 Jun 2006 at 11:06

Arto – we’ll be releasing this as a plugin to start with.

3. Comment by Ross Riley on 06 Jun 2006 at 14:06

I’d certainly give it a go, the concept was heavily outvoted on the rails list but as a personal preference I’d choose unobtrusive.

Out of interest are you going to support id based targeting only or class based as well?

Ross

4. Comment by Luke on 06 Jun 2006 at 16:06

The plugin uses the event:Selectors library by EncyteMedia – take a look at that and it will give you an idea of what you can do.

Return to home page | Check out my tumblelog

Commenting on this article is now closed