2005-09-16

Behaviour and the Javascript Prototype framework

All right, lesson learned. Always write something about interesting things you end up reading and exploring in your technical blog, or you'll end up spending needless time scratching your head trying to get back to it before long. Sometime recently I noticed the Giant-Ass Image Viewer, a somewhat more open source version of the AJAX visualization technique employed by Google Maps to break up the world into tiny bits that your browser can handle. Interesting, but not what I spent my evening reading up on.

Instead, I enjoyed the featured tech peek and zapped away to Behaviour by Ben Nolan. But don't go there to read up on it yet; Ron Lancaster wrote a great article explaining in greater clarity than Ben himself how Behaviour separates javascript from content similar to how CSS separates style from content in HTML. Add your event handlers in a neat and tidy javascript literal, mapping CSS class selectors to a set of functions applied to all matching nodes in the page, and be done with it. A keep-it-beautiful approach to integrating javascript behaviour with web pages, rather than cramming in lots of tags armed with on-something event handler attributes all over the place.

Ron also later moved on to improve further upon Behaviour, integrating it with a more capable CSS parser. I find it a bit disturbing that the DOM doesn't yet (to the best of my knowledge, anyway) let us use the core browser methods that map a CSS class selector to a matching node set, but it's at least some consolation that we can use XPath already to solve a similar set of problems.

This kind of tool would be useful to pull in when doing GreaseMonkey hacks.

The real perl I encountered during this session, however, and today wanted to get back to, also for purposes of not reinventing the wheel in every other GreaseMonkey script, was Prototype, a javascript framework developed by the Ruby on Rails team. Like any self respecting thing calling itself a framework, at least in the world of javascript whose concept of classes is a crufty mess syntactically, it provides its own little OO toolkit, but also a lot of other neat convenience goodness, like the $() alias to document.getElementById() (or a slightly improved version which can take multiple id names and return an array featuring all the corresponding nodes). Or the String methods escapeHTML and unescapeHTML, and useful DOM dribbling tools I too often reinvent on the spot, even if they are just a tiny for loop, regexp application or similar. (Interestingly, it does not have an escapeRegExp method, though, but that's done in a flash, though.)

It seems a toolkit made to have you write code that does things, readably, rather than write lots of Java namespew for your day to day needs, or make a Perl syntax mess to do the things which should have had DOM methods a browser is bound to do often, but didn't. I lack some of the tools I use a lot (notably http_get(url, callback, callback_args) and its http_post(url, vars, callback, callback_args) counterpart, passing the result page, the request object and any additional callback_args on to the callback) -- but then these are also things that can be remedied fairly easily. Minimized typing to action ratio. And it's good to build upon the works of others.

I think I'll play with Rico some time in this blog too, perhaps to clean up the spacious navigation I'm still not very happy with. All in due time, though.
blog comments powered by Disqus