Design patterns solve common programming problems. On the one hand, they are extremely useful and eliminate the need to produce code work-arounds. On the other hand, they are not always necessary and should not be used unless they are needed to satisfy a specific problem. The most commonly used patterns are the Singleton and the Model View Controller because they help solve structural problems that arise in almost every object-oriented web application.
The components we built in Part III, "Creating Reusable Components," provide a few examples of how to build your own custom components and connect them with the Ajax engine and server-side data. With this knowledge, it is possible to move forward building your own custom components to be used in multiple projects and save you development time. Components provide base functionality that can be shared as open source or across multiple applications. The great thing about the components we made in this book is that the design can easily be tweaked based on the fact that the components rely solely on CSS. Relying on CSS is always a solution that will pay off because the look of a component can be completely changed based on different CSS classes and styles that we define, making them very flexible and company specific to follow branding guidelines and so on.
Static Versus Dynamic Responses
Static responses are XML or JSON files that reside on the server, which are not generated based on dynamic data from a database or third-party source. Dynamic responses are the opposite, meaning that they are based on fluid, changing data from a database, for example. Static responses are sometimes the only option you have regardless of the best solution, such as when a client does not have a database or the ability to use a server-side language on her server. In these cases, it is usually more feasible to use XML because if a client is updating the content, it is a much easier language to understand. Static XML or JSON should be used for small single websites that do not need to be updated on a regular basis. XML is also the best cross-application language such as when trying to connect with application APIs or web services.
When creating XML or JSON responses, it is always a best practice to provide the structure with informative names for elements while keeping it as abstract as possible. Keeping the structure abstract is a great way to provide a flexible and reusable response that can easily be parsed by yourself or other developers.
Dynamic responses are the best solution for web applications or even large websites in which a client wants to maintain the content, such as with a content management system.
Error and Feedback Handling
When making server-side database connections with Ajax, we leave our database wide open. This is a huge problem because someone could simply make the right HTTP request to our application and wipe away data or overwrite it. This is why the security code in Chapter 23, "Securing Your Application," should be a required step in all your database-enabled Ajax web applications and should never be overlooked.