AJAX: What's the best tool for the job?

Choices, choices, choices. Is it better to pull XML from the server and manipulate it via DOM/XSL? Is it better to use JSON to convert data directly to JavaScript variables? Is it better to pull text in as tag soup, spackling it into place via innerHTML? Or is it better to use CSV and string.split()? There are no hard and fast rules, so I'll just convey some of my thoughts and experiences.

XML: An issue here is choosing what XML language to use. The XML language that sprang readily to my mind was, of course, XHTML. There are some pretty good reasons to use XHTML for something like this, plus rolling your own XML language might not be such a good idea. But mainly it provides options. With minimal fuss I can use DOM to drop elements from the X(HT)ML right onto the page, or I can be agnostic about things and treat it like XML; reading out data from it and transforming it as necessary. I just make sure my XHTML sticks to reasonable structure conventions. XHTML also has built-in metadata capability, a well-understood syntax, and, as of recently, microformats. All of this is gravy for me, the programmer. With this technique, there are some things to remember: first, an XHTML "mini-document" needs to be well-formed or the parser will reject it; second, the mini-document needs to use the XHTML namespace as the default namespace or the client may not treat it as XHTML.

JSON: Render unto Caesar that which is Caesar's. Thus, in my mind, XHTML is the best data format, and JSON is the best way to import dynamic behavior. Traditional JavaScript files can get huge, and given the direction things are headed, this problem will only get worse. Why not have a minimal JavaScript file, and from there load only the functionality you need, as needed? What this entails is, instead of using JSON notation like this: var foo = {"bar":"baz"}, use notation like this: var foo = {"bar":function(){/\* do stuff \*/}}. It also might make sense to implement some design patterns on top of this, for example the command pattern or some kind of inversion of control pattern. The possibilities are endless.

TAG SOUP: I'm going to resist busting on this method too much because there's always a situation where a given method is the best tool for the job. I'll just say this: importing text as tag soup and then shoveling strings into innerHTML with JavaScript is not my preferred way to do AJAX. I guess I just tend to like either pure XML or no XML at all, (but don't hold me to that!).

CSV: Sometimes the sledgehammer approach is the best approach. CSV has advantages: the syntax is compact, simple, easy to understand, easy to consume, easy to generate. For transmitting mass quantities of simple, tabular data, CSV is hard to beat.

Comments:

Post a Comment:
Comments are closed for this entry.
About

My name is Greg Reimer and I'm a web technologist for the Sun.COM web design team.

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today