Controlling the DOM is everything for a JavaScript coder. The DOM itself is unruly, like a bull facing a matador. It doesn’t like to be tamed, prodded, and definately does like to get told what to do when JavaScript is written poorly. For most, the DOM wins and the lowely JavaScripter has little to do but hack up the code and Just Make It Work. Well, as with anything in life from relationships to a car, just making it work, sucks.
Several years ago, I was asked to dynamically update select fields (aka dropdown boxes) and conditionally present data based on the intial select field value. Simple enough, eh? A little javascript, a little php, the beginnings of an Ajax app for sure. While it wasn’t around at the time, I could have used some Metaprogramming to solve the problem. In its place, I wrote what I thought at the time was really clean javascript and PHP to perform the task. Only now, I know I was wrong all along. Granted, it worked as a solution and its still running today flawlessly for the client, however, I could have done it better. Like anyone worth their salt, I’m a perfectionist and an idealist, which makes for a very very picky programmer.
Adamlogic has tweaked prototype to developed an elegant solution to my very problem. I can’t believe it, but this is dead sexy [pdf]. Adamlogic’s usage of prototype to make JavaScript programming solve realworld problems with real world syntax is just so perfect.
The benefits read like a dream for a programmer who cares:
* Seperate “What” From “How”
* Reusable
* Self-Documenting (reallly?)
* Easy to Maintain (!)
but the big one is
- Chain methods together in english language syntax.
Stringing methods like this, like passing objects as params to a method, is powerful indeed. This along could have solved a month of code.
show('US-state-field').when('country').is('United States');

