AJAX

ID Proliferation Eradication Technique #1: Leverage page.select with page.insert

In Fight id Proliferation I highlighted the prevalent misunderstanding of the prototype API’s insert_html, replace_html and friends that is evident both in the Prototype documentation and in Rails. Due to that misunderstanding, many of us are polluting our HTML with many more id attributes than would otherwise be necessary. In the original post I proposed a fix to Rails to remedy the situation and allow us to use CSS selectors to identify a target HTML element for the various calls. In this post I provide a workaround you can use now, while waiting for a cleaner fix.

Continue Reading »

AJAX
RJS Templates
Ruby on Rails
css

Comments (1)

Permalink

Fight id Proliferation and Update Any Element You Want

The Prototype Updater constructor takes an Element (object) or element id (String) as the first parameter. You can see this in prototype.js in Abstract.Insertion.initialize and Ajax.Updater.updateContent . In both situations the first parameter sent to the constructor has $() applied before use. And as I’m sure you’re aware, the effect of that is that if the parameter is an Element, then the same element is returned. OTOH, if the element is a String, then that string is assumed to be an element id and the Element is found in the DOM and returned.

The big deal here is that the documentation lies or at a minimum fails to make this point clearly. Have a look at prototype-api.pdf. And this misunderstanding is carried forward in the Rails wrapper API functionality ActionView::Helpers::PrototypeHelper::JavaScriptGenerator::GeneratorMethods#insert_html, replace_html, remove, show, hide, and visual_effect. The result of this misunderstanding is that many JavaScript programmers think they need id’s all over the place — and Rails programmers actually do need id’s all over the place (unless and until the API is repaired…)

What is wanted in the Ruby wrapper, in order to expose the full capability of the underlying JavaScript libraries, is the ability to pass not only a String (containing an Element id) but optionally to pass an instance of JavaScriptGenerator to insert_html and replace_html and to have that generator rendered. Then we could do (borrowing from the Rails doc and extending…):
    1 update_page do |page|
    2   page.insert_html :bottom, page.select(‘p.welcome b’).first, “<li>#{@item.name}</li>”
    3   page.visual_effect :highlight, ‘list’
    4   page.hide ’status-indicator’, ‘cancel-link’
    5 end

Note that instead of the simple string literal ‘list’ (from the Rails doc) we’ve got a full-fledged expression there. And generate something like this:

    1 new Insertion.Bottom($$(‘p.welcome b’).first, “<li>Some item</li>”);
    2 new Effect.Highlight(‘list’);
    3 [“status-indicator”, “cancel-link”].each(Element.hide);

But really, the proposed change to the Ruby wrapper isn’t limited to CSS selectors of course. Once insert_html, replace_html and friends support a full-fledged generator parameter, you could put arbitrary JavaScript in there. The most obvious examples would be calling custom functions and DOM traversal functions.

AJAX
Ruby on Rails
script.aculo.us

Comments (6)

Permalink

Rails IDE Goes Jurassic

About a year ago in Smalltalk Browser Goes Jurassic I lamented the fact that the Smalltalk browser UI was caught in a techno-aesthetic time warp and cheered the possibility that it might escape to the future and in doing so completely skip a whole generation of UI effort-waste and bad taste (e.g. Eclipse Rich Client Platform) and move directly to Web-technology UI currency. I ended with this:

How long will it be before a complete IDE is delivered as a web application? To varying degrees, Eclipse and IntelliJ IDEA are stuck on the same island that Smalltalk was. They’re all trying to be graphically rich and run on many platforms. They’re all expending lots of resources maintaining UI toolkits (think of Eclipse’s Rich Client Platform). And the resultant UI technology, while often innovative and sometimes pleasing, suffers a “credibility gap” when compared with platform-specific technology on the Mac or Windows. When will the IDE’s throw their weight behind the DHTML+AJAX crowd and embrace the “third platform”?

Well there is renewed hope - but it looks like Ruby and Rails may arrive before Squeak does. Gyre is an honest-to-goodness Ruby on Rails IDE delivered through the browser complete with source-level interactive debugging, project navigation, and an interesting syntax-aware text editor.

Seems like the next step is to get the Gyre folks working with the Firebug folks. Can you imagine it?

AJAX
One Step Forward
Ruby on Rails
Seaside
Smalltalk
Squeak
Web as Platform
usability

Comments (1)

Permalink