AJAX - The Complete Reference

(avery) #1

414 Part II: Developing an Ajax Library^


AjaxTCR.util.event.addWindowLoadEvent(function) that takes a passed function
reference or literal and sets it up to be loaded upon the load of the window. The following
code:

AjaxTCR.util.event.addWindowLoadEvent(function ()
{AjaxTCR.widget.ratingWidget.init()});

would simply invoke the init() method with no parameters, while here, an options object
is specified and sets appropriate values:

AjaxTCR.util.event.addWindowLoadEvent(function () {
var options = {
id: 'ratingWidget1',
question: "How do you feel about Ajax?",
url: "http://ajaxref.com/ch9/rate.php",
payloadValue : "rating"
};
AjaxTCR.widget.ratingWidget.init(options);

Within our widget init() method, a ratingsOptions object is created that stores the
options coming first from default and then being overridden/added to from the passed in
options object:

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];

Next, it ensures that form is set as an option, and then sets the defaults for our
communication to the values in the form, specifically the URL to send the rating to, the
HTTP method to use, and the argument or parameter name to form our payload with.

if (!ratingOptions.form)
return;

/* read rating form for default config */
ratingOptions.url = ratingOptions.form.action;
ratingOptions.argument = $selector("#" + ratingOptions.form.id + " input")
[0].name;
ratingOptions.method = ratingOptions.form.method;

Next, it finds the rating widget and calculates the number of ratings and saves out their
old values. The rating widget is found from the passed in option ratingOptions.
Free download pdf