Unobtrusive Javascript in Rails using Prototype

Posted on October 19, 2009

The idea with unobtrusive Javascript in to separate markup (HTML) from behavior (Javascript), just as CSS help us separate styling from markup. By disallowing Javascript in your markup, you gain a better overview of where to find what in your code base. Behavior goes to static javascript files and markup goes to template files! There are several other benefits to this technique that I will not be covered here.

As you might have heard Rails 3 is actually adopting this idea, which will spare us for lots of ugly repeating Javascript code when using Rails form and link helpers. The release of Rails 3 is still far away, so in this article I’ll show you how to implement the technique in Rails 2.

Javascript Bubbling

Posted on September 23, 2009

While working on my new game I’ve been doing a lot of Javascript code. In my quest to keep everything modular and decoupled my javascript objects often delegates some kind of responsibility to child objects, which in turn might delegate the responsibility to their child objects forming a kind of tree structure. I’ve made sure that the child objects do not know anything about their parents to decouple even more. This is implemented via the observer/subscriber pattern. The child objects just send out a notification when a certain event happens and the parent receives this if they’ve subscribed to the sending child. This results in a very flexible decoupled design – yummy!

However, this design turned out to yield quite some code duplication which needed to be cleaned up.