I have been reading a lot about web standards lately, and specially
about
unobtrusive javascript.
It essentially tells you to get rid of javascript in the view layer
((X)HTML), including event assignment.
Considering all of the default prototype Rails helpers don’t cope with
these
rules, what would be the best way to implement unobstrusive javascript
in a
Rails application ? Overriding these helper methods ?
I have found the ujs4rails plugin, but it doesn’t seem to be usable as
of
now, with Rails 2.x. The last update was back on 2006. Too bad as it
seems
to be a really cool project.
It essentially tells you to get rid of javascript in the view layer
((X)HTML), including event assignment.
That is UJS’s marketing pitch. While there’s a lot of benefit in letting
a Ruby
generator build your Javascript for you, there’s still no benefit in
hiding
absolutely all of it. If your View needs a custom onload=‘foo()’, just
go ahead
and write a little JS.
Any hints greatly appreciated!
Also, make sure your unit tests cover your JS.
(Unit tests - not “acceptance tests”. They should not raise a browser
just to
spot-check things…)
On Jul 6, 2008, at 7:07 AM, Marcelo de Moraes S. wrote:
unobstrusive javascript in a Rails application ? Overriding these
helper methods ?
I have found the ujs4rails plugin, but it doesn’t seem to be usable
as of now, with Rails 2.x. The last update was back on 2006. Too bad
as it seems to be a really cool project.
Any hints greatly appreciated!
I normally hook the loaded event in my javascript. So, for example
(Prototype):
document.observe(‘dom:loaded’, function() {
// set up event watchers, etc.
}
or for jQuery:
jQuery.document(ready){function() {
// set up event watchers, etc.
}
This does not address Philip’s concerns with respect to having
JavaScript wrapped into unit tests, but it is a viable way to hook
things up in an unobtrusive way.
document.observe(‘dom:loaded’, function() {
// set up event watchers, etc.
}
Yet note that’s still raw JS - not hidden inside a Ruby layer.
This does not address Philip’s concerns with respect to having
JavaScript wrapped into unit tests, but it is a viable way to hook
things up in an unobtrusive way.
That would spot-check the business logic went into the view at the right
spot.
That’s quite enough coverage to defend this feature from refactors and
upgrades.