AJAX - The Complete Reference

(avery) #1

186 Part II: Applied Ajax^


</script>
</head>
<body>
<h3>How do you feel about Ajax?</h3>
<form action="#" method="get">
<em>Hate It - </em> [
<input type="radio" name="rating" value="1" /> 1
<input type="radio" name="rating" value="2" /> 2
<input type="radio" name="rating" value="3" /> 3
<input type="radio" name="rating" value="4" /> 4
<input type="radio" name="rating" value="5" /> 5
] <em> - Love It</em>
</form>
<br />
<div id="responseOutput"> </div>
</body>
</html>

This is certainly much easier! If you want, verify the example works, as before, by
accessing it at http://ajaxref.com/ch5/getrevisited.html.

Adding Modern Conveniences

Now, this is not nearly the end of our library discussion. We should make sure to continue
simplifying all the annoying things that have been done in the course of sending and
receiving data. In the case of preparing data for sending, payload strings must be formed.
Recall the function serializeForm() and serializeObject() presented in Chapter 4.
Add those to the library as well. Put those under the “AjaxTCR.data” namespace so the
reference path will be AjaxTCR.data.functioname; for example, AjaxTCR.data
.serializeForm(). The function signature for serializeForm() looks like so:

serializeForm : function(form, encoding, trigger, evt) { }

The first parameter passed is a reference to the form to serialize either by its name or id
parameter or via a direct JavaScript object reference. The second parameter specfies the type
of encoding to perform, such as the standard x-www-form-urlencoded or application/
json. The next two optional parameters are the form element that is going to be the
triggering object for communications and the event parameter that triggers the call in order
to address various cross browser concerns for event handling. We omit the details of the
function as it has been covered in the previous chapter.
The serializeObject() method is a bit simpler. It allows the user to pass in an
existing payload string and object filled with values to add to the payload and an indication
of the encoding to perform.

serializeObject : function(payload, obj, encoding){ }

What is new in this library is that a special flag is created for the options object
(serializeForm) to indicate that a form should be serialized. This value should be set to
the name, id, or object reference of the form to create the payload from. For example, the
rating example might be simplified to these options:
Free download pdf