Whatever happend to unobtrusive javascript in Rails?

Don’t get me wrong, JavaScript/Ajax helpers in Rails are a huge
timesaver and they have helped me to finally overcome my irrational
aversion to js libraries like prototype and such. However, all this
goodness seems to come at quiet a price. The resulting code is
littered with inline JS, including ubiquitous script tags and onload
attributes etc… It seems that just when i found myself fully
appreciating the advantages of graceful degradation and progressive
enhancement and gotten myself into a habit of writing clean,
unobtrusive dom scripts – fully separated from the presentation
layer – i’m now again faced with with a dilemma in Rails: Here is a
framework that is all about beautiful code and strict separation, yet
i suppose to just throw out all good intentions on the client side?

I also don’t think the above concepts are superficial, contrived or
inappropriately dogmatic. There are tremendous real-life benefits and
in some cases they even become necessities. There’s also a more
metaphysical aspect - if you will - the immense satisfaction of
looking at squeaky clean xhtml source, yet “invisibly” enhanced by
advanced DOM-scripting functionality and AJAX glitz. I understand
that i’m not required to use JS helpers in Rails but i feel i would
miss out on a great deal of very productive features if i decided to
implement all this manually for the sake of progressive enhancement.

Also, it strikes me as something that could prolly be fixed rather
easily (ok, i’m being a little presumptuous), since it’s not a
limitation of Prototype etc, but the way JS helpers are implemented
in Rails. I heard a lot about RJS templates (i’m not using edge yet),
but as far as i can tell - while prolly tremendously helpful - they
seem to do nothing to overcome this particular problem.

So, my question, how have other people solved this problem? Is there
any kind of development (Core or otherwise) that will help to
eliminate inline JS yet keep within the friendly confines of JS/Ajax
helpers in Rails? Or is there simply not much interest within the
Rails community?

I apologize if this has been brought up before, but unfortunately the
mailing-list archive search is still not working and I wasn’t able to
find any relevant discussion searching on google. If this is all an
old hat, feel free to direct me to the appropriate discussion. I’m
brand new to RoR and i can only imagine that this must have come up
many times before, so i dont want to bore anybody with questions that
have been discussed ad nauseam.

Thanks.
Sebastian

Hi,

I’m about a few days of releasing a plugin which will solve your
problems and add a load of extra functionality to the rails javascript
helpers generally. As a bit of a sneak peek you can view the rdocs at
http://www.vivabit.com/unobtrusivedocs/ Don’t try to install it yet
though because it’s not production ready yet. However, it when it is
I’ll post it on this list. I’m glad that there are other people are
starting to become interested in doing javascript the right way with
Rails now. In the past any time it was mentioned it fell on deaf
ears.

Cheers,

Dan

On 3/5/06, Sebastian F. [email protected] wrote:

framework that is all about beautiful code and strict separation, yet
implement all this manually for the sake of progressive enhancement.
eliminate inline JS yet keep within the friendly confines of JS/Ajax

Thanks.
Sebastian


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Dan W.
http://www.danwebb.net

Honestly, this i find quiet shocking. Rails is all about doing things
in a clean, strictly compartmentalized manner and Rails adopters
strike me as a very open-minded, progressive community. So, why take
steps backwards and turn a deaf ear to the important advancements on
this front? I’d recommend to everybody who honestly believes this is
just trivial nitpicking to read up on blogs like PPK’s http://
www.quirksmode.org and/or seek out the fine work Jeremy Keith, Dave
Shea and others have contributed to this subject.

Sebastian

I can think of a couple reasons why this isn’t part of rails:

  • It’s not nearly as straight forward. Instead of using
    link_to_remote, you have to put some code in the head of the document
    or your behavior sheets.
  • Browser issues. Setting the onclick of an anchor works great.
    However, adding event listeners doesn’t work because safari can’t stop
    the actual click from happening. This is probably a moot point if you
    just use $(‘link’).onclick = function() { …; return false; }.
  • No one’s been able to demonstrate concrete code for it.

Dan: that plugin looks great. It would be nice to see it’s API match
the RJS API, which has been going through major changes. You can now
do:

page.select(‘#items a’).each do |anchor|
anchor.add_class_name(‘foo’)
end

I currently use Behavior sheets for Rails Weenie. Everything is page
cached, so I have a request to a session javascript file that loads
the appropriate sheet. Its all in javascript, but I was hoping to
move it to RJS as part of a rendered view.

http://techno-weenie.net/svn/projects/rails_help/public/javascripts/rails_help.js


Rick O.
http://techno-weenie.net

On Mar 5, 2006, at 5:21 PM, Dan W. wrote:

I’m about a few days of releasing a plugin which will solve your
problems and add a load of extra functionality to the rails javascript
helpers generally. As a bit of a sneak peek you can view the rdocs at
http://www.vivabit.com/unobtrusivedocs/ Don’t try to install it yet
though because it’s not production ready yet. However, it when it is
I’ll post it on this list.

Dan, this is absolutely fantastic. Can’t wait for this to be
officially released (any preliminary timeline?). In fact, i doubt i
will do anything production-wise with Rails until your plugin can be
used in a project. If this works as advertised, this would be an
incredibly invaluable contribution. Please keep up the great work and
let us know as soon you ready for people to start testing it.

I’m glad that there are other people are
starting to become interested in doing javascript the right way with
Rails now. In the past any time it was mentioned it fell on deaf
ears.

Honestly, this i find quiet shocking. Rails is all about doing things
in a clean, strictly compartmentalized manner and Rails adopters
strike me as a very open-minded, progressive community. So, why take
steps backwards and turn a deaf ear to the important advancements on
this front? I’d recommend to everybody who honestly believes this is
just trivial nitpicking to read up on blogs like PPK’s http://
www.quirksmode.org and/or seek out the fine work Jeremy Keith, Dave
Shea and others have contributed to this subject.

Sebastian

Hi,

Essentially some of Rick’s work on the JavascriptProxy class in 1.1
collided with what I’m doing so my plan has been to stall until 1.1
(…hmm, that would be now) then refactor what I have on top of 1.1
and the Rick’s work which shouldn’t take too long. I got some
valuable feedback from DHH and Rick which I plan to act on.

The good news is that there’s been some work on Prototype that makes
the whole thing alot easier to manage. Bear with me, proper plugin
will be released soon.

Cheers,

Dan

On 3/28/06, Sebastian F. [email protected] wrote:

Honestly, this i find quiet shocking. Rails is all about doing things
I can think of a couple reasons why this isn’t part of rails:
Dan: that plugin looks great. It would be nice to see it’s API match
move it to RJS as part of a rendered view.
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Dan W.
http://www.danwebb.net

Dan,

any progress on this? i saw the documentation was updated. does this
mean it’s ready to go?

thanks,
sebastian

Thanks for the update, Dan.

Solving this still seems like a very important issue. Besides the
problems implied, there’s also the annoying side effect of js helpers
simply messing up well-formedness and validity in certain contexts.
For instance, javascript helpers placed within lists (ul, ol, dl)
will render pages invalid XHTML (strict):

>Name:
<%= in_place_editor_field :profile, :name, {:tag => "dd"} %>

This (common enough) approach will place the script tags within the

element, which will fail XHTML strict validation.

As i have gotten myself a little more aquatinted with Rails, i
started playing around a bit to at least move specific code generated
by the inline helpers to external files. This worked to a certain
degree but in the end it’s only a newbie attempt and stopgap for now.
Your plugin, however, seems to be shaping up to be a real solution.

Ideally, the plugin would be so low-level that it can work fully
transparent in the background and not require any new declarations,
changes in RJS or js helper syntax. Is that even possible? and, if
yes, is that the route you are taking? Can you enlighten us a little
how the re-worked plugin will be implemented?

As you can see, i’m really looking forward to this :slight_smile: I really
appreciate your work.

Sebastian

if i complain about js helpers messing up validity, i should at least
give an otherwise valid example :). Sorry.

It should be:

Name:
<%= in_place_editor_field :profile, :name, {:tag => "dd"} %>

sebastian