AJAX - The Complete Reference

(avery) #1

450 Part II: Developing an Ajax Library^


You can see such an approach working at http://ajaxref.com/ch9/ratingwidgettag.php.
When you view the source, you will see that the script code and markup are indeed all
there, but we made the developer feel they weren’t.
The tag and template crowd might be happy with the previous wrapper, but coders may
prefer a different approach. They may want to use a snippet of code, create an object, or call
a function to do the same job. Instead of a tag, they would want something like what we
see here:

<?php
require_once "widgetcode.php";

$options = array("min"=>1, "max"=>5, "minstring"=>"Blech",
"maxstring"=>"Love them!", "url"=>"rate.php");
$rating = new RatingWidget($options);
$rating->setQuestion("How do you feel about widgets?");
$rating->setPayloadValue("rating");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Chapter 9 : Architecture - In Page Ajax - Code Based Widget</title>
<?php print $rating->getScript(); ?>
</head>
<body>
<?php print $rating->get(); ?>
</body>
</html>

We note the bold text to show what is important which shows an emphasis on server-
side coding, but it is no different than the previous custom tag example and the same issue
applies here: the developer doesn’t see the JavaScript or even the XHTML used, but it is
there and when it renders you see the same underlying markup and script.
See for yourself at the demo http://ajaxref.com/ch9/ratingwidgetcode.php.
Free download pdf