JQuery_ Novice to Ninja - Earle Castledine [149]
StarTrackr!: Epilogue
“We stand here today as creators of the most notorious celebrity hunting web site on the planet!” booms the client to a room full of employees and shareholders. The final phase of the project is live, and your work is coming to an end. It sure has been a long journey since our client asked us to spruce up his site’s disclaimer message—a journey that has taken us through fading and sliding, easing and scrolling, enhancing and Ajaxifying. A journey that has taken us, in other words, from novice to ninja.
“But there was one person who made all of this possible,” the client continues. “One person we owe all of this to …” and he turns to face you, but the space you’d previously occupied is empty. You’ve performed one last $("#ninja").hide()—and mysteriously vanished without a trace.
You have no need for the thanks and praises of your clients and co-workers; your reward is that good web development practices have been instilled into yet another project. That, and the significant payments you received. A bit of both, really.
Appendix A. Reference Material
jQuery is all about being flexible, and applicable to as many situations as possible. Both the core library and the plugin architecture encourage this philosophy. The most common usage scenarios are usually catered for straight out of the box, so jQuery’s flexibility rests in the ability to override those defaults. This results in a lot of options! There’s no need to memorize them all, though—just have a good reference on hand, and always check jQuery’s online documentation. And don’t be afraid to dive into the jQuery or plugin source code to look for anything the documentation may have missed.
$.ajax Options
The powerful array of Ajax functions in jQuery is underpinned by a single method: $.ajax. This method accepts a plethora of options, giving it the flexibility to be used in countless situations. We examined some of the options throughout the book, but as always with jQuery, there’s more!
Flags
The “flaggable” options accept a Boolean value—true or false—to enable or disable the given functionality. Most of the time the defaults will be satisfactory, but it’s easy to override them to customize your request.
async
The async option is true by default, but if you need to do a synchronous request (which you should try to minimize—it locks the browser while it’s working!), you can set this to false.
cache
Caching data can be an issue when performing Ajax requests; if a user’s browser stores old requests, it might not fetch the latest data. To disable caching, you can set the cache option to false. Script and JSONP requests are uncached by default.
global
We saw that we could handle events from any Ajax request using global event handlers. You also have the option of stopping global event handlers from handling a particular event by specifying false for the global parameter.
ifModified
By setting the ifModified flag, you can force a request to be “successful” (that is, to fire the success handler) only if the document was modified since the last request. This can be useful if you only want to do some processing when there’s new data to display.
processData
When you have data to send along with your request, jQuery will process it and convert it to an appropriate query string value. If this is undesirable (for example, if you need to pass a DOM document, or some other sort of structure) you can set the processData flag to false.
Settings
Many of the Ajax options allow you to specify more than just a simple on/off value; they generally accept strings or objects to customize and define your Ajax request.
contentType
The contentType setting allows you to set the content type for the request. By default it’s application/x-www-form-urlencoded, which is fine for most cases.
context
When Ajax callbacks execute, their context is the window