AJAX - The Complete Reference

(avery) #1

442 Part II: Developing an Ajax Library^


request.template is the URL of the template of interest and is appended to the request
payload (query string or message body) so that the server can render the response. We will
need to make sure that the server program is defined with this in mind. We could also set
template: dynamic and templateRender: server, but this would be the standard
method that is employed in a server-side program so there is nothing for the client library
to do here.

Summary of Library Template Mechanism
We present a brief summary of each template mechanism in Table 9-1 as an overview.
We have also provided a simple Template Explorer program for you to play with at
http://ajaxref.com/ch9/templateexplorer.html, with the general idea of the example shown
in Figure 9-6.

Applied Templates


Architecturally, templates certainly can clean things up quite a bit. As we have shown, if the
application is well designed, the presentation layer can be elegantly broken out into an Ajax
and non-Ajax approach and even switch back and forth. Templates also may have some
other value in that they provide a degree of flexibility. For example, consider how we might
extend our rating widget to display a different response template depending on the type of

Template
Specified By

Render
Location Description Example
Client Client Library fetches given
template file. When response
and template file return, it
processes the output.

http://ajaxref.com/ch9/templatecc.html

Client Server Library sends the URL of the
template to the server in
the payload with a name of
templateURL.

http://ajaxref.com/ch9/templatecs.html

Server
(returning
URL and
template
string)

Client Server generates both the
template contents as well
as the URL key it should be
stored as in the template
cache. These are passed
back in the JSON packet
as templateText and
templateURL.

http://ajaxref.com/ch9/templatesc3.html

Server
(returning
URL)

Client Server generates the URL
and passes it back as part
of the JSON packet as
templateURL. The library
then sends a synchronous
request to get the template
and caches the results.

http://ajaxref.com/ch9/templatesc2.html

TABLE 9-1 Summary of Template Usage Patterns
Free download pdf