Rails XMLHttpRequest Done Well

In the excellent book Editing Line-Items Efficiently, Dave Goodlad goes through his develpment process for using some XMLHttpRequest elements to streamline a Rails application.

Having done the same exercise some time ago, I have to say I was highly impressed with Dave's methodology and the 'clean-ness' of his design. I wrote up a little app to perform almost exactly the same function (editing users) with almost exactly the same UI (table view, click an edit button to replace text with form fields, and a save button), but I was far too heavily reliant on Javascript.

The JS was all nice and abstracted, but relied on some form naming conventions to properly munge the DOM objects, obviously less than ideal. Dave's example drives home the following points:

  • You already have an excellent framework available in Rails
  • Said framework is really nice at templating and HTML generation
  • The JS to perform the request calls should be as simple as possible
XMLHttpRequest (in theory) returns XML. Imagine this as XHTML, and all of a sudden your request just grabs a string and sets that string as the id.innerHTML of a parent object. Beautiful! No convoluted iterations over JS elements, and you get the power of Rails to generate the HTML for you.

Of course, this is only one way to do it, but I found it to be far superior to my initial foray in the same direction. Time to go download it and implement his methods myself, maybe throw in some error checking & timeout handling, just in case.

Don't try to do the heavy lifting in JavaScript, or even light lifting, keep it is an intermediary transport. Let the far superior middleware handle your lifting needs.

Categories

0 TrackBacks

Listed below are links to weblogs that reference this post: Rails XMLHttpRequest Done Well.

TrackBack URL for this entry: http://andywismar.com/cgi-bin/mt-tb.cgi/295

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)