AJAX - The Complete Reference

(avery) #1

PART II


Chapter 9: Site and Application Architecture with Ajax 429


question: "How do you feel about Ajax?",
url: "http://ajaxref.com/ch9/rate.php",
payloadValue : "rating"
};
AjaxTCR.widget.ratingWidget.init(options););

if (!AjaxTCR.widget)
{
AjaxTCR.widget = {};
}
AjaxTCR.widget.ratingWidget = {
defaults : {
minRating: "Hate it!!!!",
maxRating: "Love it",
choices: 5,
choiceOff: "star_off.gif",
choiceOn: "star_hover.gif",
choiceSelected: "star_reg.gif",
method: "POST"
},
init : function (options) {
/* create ratingOptions Object */
var ratingOptions = {};

/* set defaults */
for (var anOption in AjaxTCR.widget.ratingWidget.defaults)
ratingOptions[anOption] = AjaxTCR.widget.ratingWidget.defaults[anOption];

/* Set/Override Options */
for (var anOption in options)
ratingOptions[anOption] = options[anOption];

We also note that the markup is created using DOM functions, which can get bulky:

var widget = $id(ratingOptions.id);
/* set the question */
var question = document.createElement("h3");
question.innerHTML = ratingOptions.question;
widget.appendChild(question);

/* set the min ranges */
var minRating = document.createElement("em");
minRating.innerHTML = ratingOptions.minRating;
widget.appendChild(minRating);

/* create the ratings container */
var ratings = document.createElement("span");
ratings.id = ratingOptions.id + "ratings";
widget.appendChild(ratings);

ratingOptions.prefixID = AjaxTCR.util.misc.generateUID("AjaxTCRRatingChoice");

/* add the images to the rating container */
for (var i = 0 ; i < ratingOptions.choices; i++)
{
Free download pdf