AJAX - The Complete Reference

(avery) #1

PART II


Chapter 9: Site and Application Architecture with Ajax 445


In most of these demos, including the last one, templates provide a degree of flexibility
and architectural elegance that should make changing look easier, but they do so typically
with extra requests. As we saw in Chapter 6, making extra requests is not desirable—but are
these examples a bit misleading? We think so. Consider that in many Ajax applications, the
user will stay on the same page for quite some time performing various tasks. For example,
imagine if there was a data access system where the user could page through large data sets,
say, 50 items at a time. With a client-side template, the template to hold the data in the form
of a table is downloaded one time and then it can be populated over and over again with
received data. In this scenario, the Ajax with templates-style architecture has clear
advantages from a delivery point of view because we avoid sending structural information
over and over and again as you might with an Ajah or Ajax pattern and will offer significant
savings over the traditional pattern. We can see just such an application of templates at
http://ajaxref.com/ch9/paging.html, which is shown Figure 9-7.
With this last example, we can imagine adding in features to manipulate the data and
building a full Ajaxified CRUD (Create Read Update Delete) system. Before we do that, let’s
add one more abstraction to push the architectural limits of Web applications.

Wrapping the Widget


In our examples so far, you have seen a fair amount of JavaScript code being included.
We’ve done our best to make the rating widget robust and elegant, but frankly, developers
who want to use the rating widget probably don’t care about all the work that went into it,
they just want to use it. In fact, depending on the developer, they may want absolutely
nothing to do with the JavaScript utilized or even to know that it is there! There are ways to
hide these details from people if we decide to wrap the widget.
Consider making the rating widget friendly to those who like to build templates, that is,
those who find < > to be pleasing characters. Maybe they would like to simply insert some
markup like this into their page:

<AjaxTCR:widget name="rating" question="How do you feel about widgets?"
max="5" maxString="Love them!" min="1" minString="Bleech" url="rate.php"
payloadValue="rating" />

They might also be willing to insert a tag that tells them to include all the necessary
libraries, like this:

<AjaxTCR:includeLib>

Although, having seen this a few times, we wonder what the big deal is as compared to:

<script src="http://ajaxref.com/ch9/ajaxtcr.js" type="text/javascript"></script>

No matter, we can enable custom tags to insert the Ajaxified widgets. Depending on the
development environment you use, this may be provided directly, or you may have to write
a bit of code to find the tag and replace it—the point is the approach. Roughly, when the tag
approach is used, the widget example might become something like:

<?php
require "widgettag.php";
?>
Free download pdf