AJAX - The Complete Reference

(avery) #1

PART II


Chapter 9: Site and Application Architecture with Ajax 439


Now we might wonder about the situation that different templates need to be in play
for different reasons such as browser, time of day, user, or just random variation. It might
not be appropriate to specify the template in a hard-coded fashion in the JavaScript, so we
could indicate that the template will be set dynamically by the server.

var url = "http://ajaxref.com/ch9/ratetemplate.php";
var options = { method: "POST",
payload : "rating="+rating,
template : "dynamic",
templateRender : "client",
outputTarget : "ratingResult"
};
AjaxTCR.comm.sendRequest(url,options);

On the server side, there are a few choices of how to return the template. Our library has a
convention for dynamic templates that in the JSON packet, it will expect to see two properties:
templateURL, which will be the URL of the template included or potentially blank, and
templateText, which will be the text of the template if it is included or also blank.
Free download pdf