My last post covered the topic how ActiveScaffold and Unobtrusive Javascript may fit together. This time I would like to give you an example.
It s Activescaffold’s TinyMCE Bridge. It s a perfect example, cause it s a pure javascript feature.
Let’s take a look at the bridge file using obtrusive javascript:

Ok, we add a javascript code snippet to activescaffold includes in case using_tiny_mce? returns true. Javascript code will link into action link.close action management and will remove our tiny_mce_editor. To be honest I do not like that solution at all. 🙂

A listener to activescaffold form_unload event, finding all textareas and removing tinyMCE control.

Summary

Did we achieve our goals?
We ve separated javascript code from ruby code. which improves code quality and maintainability. You do not have to be an activescaffold expert anymore to add javascript functionality to forms, form_columns, list_columns. You just need to know javascript.

Rails 3.0 started using unobtrusive javascript. However, what does that actually mean for activescaffold? Well, basically it means that there should nt be any javascript code in our partials. Looks easy at the first glance, however, if you start actually to implement it..
Big advances of unobtrusive javascript are:

application works without javascript

html code seperated from javascript code

html id attribute is nt needed anymore for many operations

I ve mentioned at the beginning that going unobtrusive is nt that easy as it seems. The big issues you have are:

How to cleanly separate javascript code?

How may application work if javascript is disabled?

It s easy to manage click events, unobtrusively, but how to manage load events for ajax calls?

In this post I will focus on the last bullet point. It took a while until I was able to manage that issue.
Unobtrusive javascript needs a trigger event to add the javascript functionality. For example when you click a button. However, if you would like to add javascript functionality on load you are in kind of trouble for ajax applications, because load events of browsers do only fire if the whole page is loaded and not if snippets of html code are changed or added during an ajax call.

I ve solved that issue at least for activescaffold as follows.

ActiveScaffold will trigger the following events in case of page load and ajax load:

as:list_row_loaded

as:form_loaded

as:form_element_loaded

ActiveScaffold will trigger the following events in case of page unload and ajax unload:

as:list_row_unloaded

as:form_unloaded

as:form_element_unloaded

A simple use case would be the following: list columns may be configured for inplace editing, which is a pure javascript feature including the need to add it to the specified columns during as:list_row_load event:

Today I was called by a user stating that something really bad happened. He clicked an actionlink accidently and now data is a mess.
Well after some investigation I ve asked him: Why did you even press ok in confirmation dialog?
The answer was quite a surprise for me cause he told me that there was nt any confirmation dialog.
First thought of any developer, user is lying, but well sadly I have to admit that he was right. He had javascript disabled and I had to accept that apparently activescaffold was performing the action without a confirmation.

I ve fixed that today. If you update your activescaffold to latest version, you are save.
However, application will display missing routing error in these special cases.

Let me show you how you may change your application to work as expected if javascript is disabled.
In my post Actionlink response and ignore I ve described an easy way to respond to actions triggered by an actionlink. I will use it as a base for my example.

First step: Adding a new route

put :fire, : on => :member
get :fire, : on => :member

As an alternative you might also add non resource routes, it s important to add them BEFORE the resource routes.