Ajax has taken the web development community by storm, giving web developers the potential to create rich user-centered Internet applications. But Ajax also adds a new level of complexity and sophistication to those applications. Ajax in Practice tackles Ajax head-on, providing countless hands-on techniques and tons of reusable code to address the specific issues developers face when building Ajax-driven solutions.
After a brief overview of Ajax, this book takes the reader through dozens of working examples, presented in an easy-to-use solution-focused format. Readers will learn how to implement rich user interfaces, including hands-on strategies for drag and drop, effective navigation, event handling, form entry validation, state management, choosing Ajax libraries, interfacing to open web APIs, and more!
Unlike the traditional cookbook approach, Ajax in Practice provides a thorough discussion of each technique presented and shows how the individual components can be connected to create powerful solutions. A fun mashup chapter concludes the book. Throughout the book, the examples chosen are interesting, entertaining, and above all, practical.
With this book you will
This book is aimed at web developers who want their applications to be best-in-class examples of rich user interfaces, leveraging Ajax technology to achieve this goal.
While novices to Ajax will find the first two chapters helpful in getting kick-started into the world of asynchronous requests, this book is primarily aimed at developers with at least a basic background in developing web applications and in the rudimentary use of JavaScript to effect client-side activity.
In the new world of rich client-side user interfaces, the amount of client-side code has greatly increased and it is important to treat this code with the same level of respect due its server-side counterpart. Advanced JavaScript techniques that help to organize this client-side code and to use Ajax effectively are presented in this book.
If you are a web developer interested in expanding your coding skills not only with new technologies, but also with techniques and patterns that make the best use of that technology, we think that you will find that this book addresses those needs.
Whether you are a seasoned client-side developer, or one that is just starting out creating rich user interfaces to your web applications, we hope this book will have something for you.
Weve divided this book into two parts. Part 1, Fundamentals of Ajax, includes four introductory chapters that make sure that youve got the know-how under your belt that youll need to make best use of the second part of the book. The chapters in part 2, Ajax Best Practices, present various practical topics in client-side programming, with an emphasis on using Ajax directly, or on practices and principles that work well in Ajax-enabled applications.
Chapter 1 dives head first into what makes Ajax different from other technologies and why theres so much to be written (and learned) about it. It presents a crash course in using Ajax across the various browsers and how to deal with the responses it generates. Finally it closes with a brief look at how use of the Prototype library makes the whole process more streamlined.
In chapter 2, we examine the various categories of Ajax communication including JSON, XML and XSLT. Well also investigate the use of Ajax with SOAP web services.
Chapter 3 introduces the concept of using object-oriented JavaScript to take control of the increasing amount of client-side code that the typical Ajax application contains. Key concepts such as the object construction, functions as first class objects, functions as class methods, function contexts, as well as closures are explained and put into perspective in relation to object-oriented techniques. Use of the Prototype library to help easily define JavaScript classes closes out this chapter.
Chapter 4 continues our investigation of Ajax-enabled JavaScript libraries with a closer look at Prototype, as well as the Dojo Toolkit, jQuery, and DWR libraries. While it would be impossible to cover the complete feature set of all these offerings, each is examined with particular attention to what they bring to the Ajax party. Well see each of these libraries put into practice in the copious code examples in the remaining chapters.
The world of event handling is examined in chapter 5. The various event models are investigated with particular emphasis on cross-browser issues, along with the use of the Prototype library to ease those cross-browser pains. The most commonly used event types are discussed in relation to how they fit into Ajax applications.
Chapter 6 dives into the details of data entry validation of form data and how it ties into the event handling lessons of chapter 5. Both the Prototype and jQuery libraries are used to great advantage in the examples of this chapter, which include demonstrating how to hijack form submissions that would usually initiate a full-page refresh, and redirect them to less-intrusive Ajax requests.
In chapter 7, the subject of content navigation is addressed. Well examine the creation of simple menus, and then progress to more complicated navigational aids such as tree views, accordion controls, tab views, and toolbars. The aid of libraries such as OpenRico and qooxdoo is enlisted by the code in this chapter.
Chapter 8 focuses on the mine field of problems created when users use back and refresh. Well look at the problem both from the point of view of removing such abilities from the user, as well as working with such actions. This chapter also discusses adding a handy undo facility to applications.
Drag-and-drop operations are the topic of chapter 9. Well examine the mechanics of drag and drop sequences, and discuss support for drag and drop in JavaScript libraries. We explore the use of Scriptaculous for manipulating lists, and develop a simple shopping cart implementation using Scriptaculous and ICEfaces.
In chapter 10, we discuss usability considerations and look at how Ajax can help us solve, or at least alleviate, latency issues. Reducing user frustration by providing server-assisted pro-active help is examined, and another look at validating form data is taken. Dealing with tab and stacking order in the new arena of rich user interfaces is also addressed.
Chapter 11 covers state management. Well explore how to maintain client state, cache data, prefetch data, and how to persist the client state. We also discuss using the AMASS library to persist large amounts of data.
In chapter 12 we delve into the exciting world of open APIs on the web. We learn how to avoid the dreaded Ajax security sandbox in order to make Ajax requests to remote servers. We then use that knowledge to make use of open APIs such as Yahoo! Maps, Geocoding and Traffic, the Google search engine, and Flickr photo services.
Chapter 13 culminates the book with a full mashup application that employs the open APIs we investigated in chapter 12, as well as the skills and techniques gathered throughout the book, to create a complete and working mashup application.
All source code in listings or in text is in a fixed-width font like this to separate it from ordinary text. Method and function names, properties, XML elements, and attributes in text are presented using this same font.
In many cases, the original source code has been reformatted: weve added line breaks and reworked indentation to accommodate the available page space in the book. In rare cases even this was not enough, and listings include line-continuation markers. Additionally, many comments have been removed from the listings.
Code annotations accompany many of the listings, highlighting important concepts. In some cases, numbered bullets link to explanations that follow the listing.
Source code for all of the working examples in this book is available for download from http://www.manning.com/crane2 or http://www.manning.com/AjaxinPractice.
Purchase of Ajax in Practice includes free access to a private web forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the authors and from other users. To access the forum and subscribe to it, point your web browser to http://www.manning.com/crane2 or http://www.manning.com/AjaxinPractice. This page provides information on how to get on the forum once you are registered, what kind of help is available, and the rules of conduct on the forum.
Mannings commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the authors can take place. It is not a commitment to any specific amount of participation on the part of the authors, whose contribution to the books forum remains voluntary (and unpaid). We suggest you try asking the authors some challenging questions, lest their interest stray!
The Author Online forum and the archives of previous discussions will be accessible from the publishers website as long as the book is in print.
The figure on the cover of Ajax in Practice is a Sultana, a female member of a sultans family; both his wife and his mother could be addressed by that name. The illustration is taken from a collection of costumes of the Ottoman Empire published on January 1, 1802, by William Miller of Old Bond Street, London. The title page is missing from the collection and we have been unable to track it down to date. The books table of contents identifies the figures in both English and French, and each illustration bears the names of two artists who worked on it, both of whom would no doubt be surprised to find their art gracing the front cover of a computer programming book...two hundred years later.
The collection was purchased by a Manning editor at an antiquarian flea market in the Garage on West 26th Street in Manhattan. The seller was an American based in Ankara, Turkey, and the transaction took place just as he was packing up his stand for the day. The Manning editor did not have on his person the substantial amount of cash that was required for the purchase, and a credit card and check were both politely turned down. With the seller flying back to Ankara that evening the situation was getting hopeless. What was the solution? It turned out to be nothing more than an old-fashioned verbal agreement sealed with a handshake. The seller simply proposed that the money be transferred to him by wire and the editor walked out with the bank information on a piece of paper and the portfolio of images under his arm. Needless to say, we transferred the funds the next day, and we remain grateful and impressed by this unknown persons trust in one of us. It recalls something that might have happened a long time ago.
The pictures from the Ottoman collection, like the other illustrations that appear on our covers, bring to life the richness and variety of dress customs of two centuries ago. They recall the sense of isolation and distance of that periodand of every other historic period except our own hyperkinetic present.
Dress codes have changed since then and the diversity by region, so rich at the time, has faded away. It is now often hard to tell the inhabitant of one continent from another. Perhaps, trying to view it optimistically, we have traded a cultural and visual diversity for a more varied personal life. Or a more varied and interesting intellectual and technical life.
We at Manning celebrate the inventiveness, the initiative, and, yes, the fun of the computer business with book covers based on the rich diversity of regional life of two centuries agobrought back to life by the pictures from this collection.