apress ajax and rest recipes, a problem - solution approach (2006)

358 729 0
apress ajax and rest recipes, a problem - solution approach (2006)

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

this print for content only—size & color not accurate spine = 0.838" 360 page count BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® Ajax and REST Recipes: A Problem-Solution Approach Dear Reader, Ajax and REST Recipes: A Problem-Solution Approach serves all of your Ajax needs by providing adaptable solutions for common tasks you’ll want to imple- ment on Web sites using the next generation of Ajax and REST techniques. As a developer, your time is precious, and you want to solve problems that present themselves in your work as quickly as possible. You can do so by adapting the code in this book for your own applications. To write an effective Ajax application, you need to understand two topics: JavaScript and REST. These topics make up the core of this book, along with the practice of writing applications using Ajax and REST Web services to decouple the client from the server, making it possible for you to write systems that can be the basis for mashups and other clients that may not be Ajax based. Throughout the first half of the book, I present solutions to many isolated issues encountered when architecting and developing Ajax applications, such as performing unit tests and test-driven development; implementing error and exception handling; defining REST Web service contracts; creating JavaScript mixins, generics, and code blocks; performing data validation; and creating dynamic layouts. The second half of the book takes a slightly different approach, combining several recipes that work in context together into larger projects and giving you a taste of how to implement real-world Ajax solutions. You’ll create a powerful Ajax shopping cart to give you much greater flexibility and control over data access than traditional solutions, a universal stock ticker application that can handle several different flavors of data source in real time, and much more. So have a look through this book’s table of contents. If you are a serious web application developer, the topics covered in this book will surely appeal to you. Christian Gross Author of How to Code .NET: Tips and Tricks for Coding .NET 1.1 and .NET 2.0 Applications Effectively Ajax Patterns and Best Practices Foundations of Object- Oriented Programming Using .NET 2.0 Patterns US $44.99 Shelve in Web Development User level: Intermediate–Advanced Gross Ajax and REST Recipes THE EXPERT’S VOICE ® IN WEB DEVELOPMENT Christian Gross Ajax and REST Recipes A Problem-Solution Approach CYAN MAGENTA YELLOW BLACK PANTONE 123 CV ISBN 1-59059-734-6 9 781590 597347 54499 6 89253 59734 7 Companion eBook Available Expert techniques to solve all your Ajax and REST architecture and development problems www.apress.com SOURCE CODE ONLINE Companion eBook See last page for details on $10 eBook version THE APRESS ROADMAP Pro Ajax and Java Frameworks Beginning Ajax with PHP Foundations of Ajax Pro Ajax and the .NET 2.0 Platform Ajax and REST Recipes Ajax Patterns and Best Practices Ajax and REST Recipes A Problem-Solution Approach Christian Gross 7346ch00FM.qxd 11/20/06 2:48 PM Page i Ajax and REST Recipes: A Problem-Solution Approach Copyright © 2006 by Christian Gross All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-59059-734-7 ISBN-10 (pbk): 1-59059-734-6 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Chris Mills Technical Reviewers: Nick McCollum, Bernhard Seefeld Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick, Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Jim Sumser, Keir Thomas, Matt Wade Project Manager: Beth Christmas Copy Edit Manager: Nicole Flores Copy Editors: Nicole Abramowitz, Nicole Flores Assistant Production Director: Kari Brooks-Copony Production Editor: Laura Esterman Compositor: Kinetic Publishing Services, LLC Proofreader: Liz Welch Indexer: Ann Rogers Artist: April Milne Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://www.apress.com. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com in the Source Code/Download section. You will need to answer questions pertaining to this book in order to successfully download the code. 7346ch00FM.qxd 11/20/06 2:48 PM Page ii Contents at a Glance About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . viii About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi ■CHAPTER 1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 ■CHAPTER 2 JavaScript Recipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 ■CHAPTER 3 Dynamic Content Recipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 ■CHAPTER 4 Implementing an SOA Architecture . . . . . . . . . . . . . . . . . . . . . . . 193 ■CHAPTER 5 Implementing a Universal Web Service Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 ■CHAPTER 6 Implementing Web Services for Large or Slow Data Sets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 ■CHAPTER 7 Implementing an Ajax Shopping Cart . . . . . . . . . . . . . . . . . . . . 297 ■CHAPTER 8 Don’t Submit Your Forms—Ajax Them . . . . . . . . . . . . . . . . . . . 319 ■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 iii 7346ch00FM.qxd 11/20/06 2:48 PM Page iii 7346ch00FM.qxd 11/20/06 2:48 PM Page iv Contents About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . viii About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi ■CHAPTER 1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Understanding the Definition and Philosophy of Ajax . . . . . . . . . . . . . . . . . . 1 Understanding the Definition and Philosophy of Web Services and SOA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Understanding the Definition and Philosophy of REST . . . . . . . . . . . . . . . . . 9 The Easiest Way to Get Started with Ajax and REST . . . . . . . . . . . . . 16 Implementing an Ajax and REST Application Using Test-Driven Development Techniques . . . . . . . . . . . . . . . . . . . . . . . 18 Coding the Contract Using Test-Driven Development Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Testing a Dynamic Contract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Testing the Client-Side Logic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Managing Ajax Security and Intellectual Property . . . . . . . . . . . . . . . 44 ■CHAPTER 2 JavaScript Recipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Understanding JavaScript and Types . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Coding Using Conventions and Not Configurations . . . . . . . . . . . . . . 52 Using Parameterless Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Treating Functions Like Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Implementing an Error and Exception Handling Strategy . . . . . . . . . 65 Understanding the Behavior of Variables When Implementing Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Using Functions to Initialize and Make Decisions . . . . . . . . . . . . . . . 78 Understanding the Ramifications of Duck-Typed Code. . . . . . . . . . . 82 Implementing JavaScript “Generics” . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Managing Runtime Behavioral Code . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Putting XMLHttpRequest into a Factory . . . . . . . . . . . . . . . . . . . . . . . . 92 Defining and Extending Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 v 7346ch00FM.qxd 11/20/06 2:48 PM Page v Implementing Code Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Turning toSource into a Complete Serialization Solution . . . . . . . . 104 Implementing Mixins in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Implementing Proxy Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Implementing Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Implementing Overloaded Methods . . . . . . . . . . . . . . . . . . . . . . . . . . 128 ■CHAPTER 3 Dynamic Content Recipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Validating Your Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Creating Dynamic Layouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Manipulating Dynamic Content Blocks . . . . . . . . . . . . . . . . . . . . . . . 159 Implementing “Dialog Boxes” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Serializing HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Dealing with Formatted Data and Forms. . . . . . . . . . . . . . . . . . . . . . 185 ■CHAPTER 4 Implementing an SOA Architecture . . . . . . . . . . . . . . . . . . . . . . . 193 Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Solution: Re-architecting the Application. . . . . . . . . . . . . . . . . . . . . . 196 Testing the Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Implementing the Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 Recipe Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 ■CHAPTER 5 Implementing a Universal Web Service Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Solution Part 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 Solution Part 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 Recipe Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 ■CHAPTER 6 Implementing Web Services for Large or Slow Data Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 Solution Variation: (Nearly) Real-Time Data. . . . . . . . . . . . . . . . . . . . 291 Recipe Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 ■CONTENTSvi 7346ch00FM.qxd 11/20/06 2:48 PM Page vi 0c8b62c78daaa2d101c6afa8a1dc3480 ■CHAPTER 7 Implementing an Ajax Shopping Cart . . . . . . . . . . . . . . . . . . . . 297 Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 Recipe Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 ■CHAPTER 8 Don’t Submit Your Forms—Ajax Them . . . . . . . . . . . . . . . . . . . 319 Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 Recipe Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 ■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 ■CONTENTS vii 7346ch00FM.qxd 11/20/06 2:48 PM Page vii viii About the Author Many people say that by looking at a person’s dog, you can tell what the person is like. Well, the picture of me is my dog Louys, an English Bulldog. And yes, my English Bulldog and I have many common characteristics. But what about my biography? It’s pretty simple: I am a guy who has spent oodles of time strapped to a chair debugging and taking apart code. In fact, I really enjoy this business we call software development. I have ever since I learned how to peek and poke my first bytes. I have written various books, including Ajax Patterns and Best Practices and How to Code .NET, all available from Apress. These days I enjoy coding and experimenting with .NET, as it is a fascinating environment. .NET makes me feel like a kid opening a present on Christmas morning. You had an idea what the gift was, but you were not completely sure. And with .NET, there is no relative giving you socks or a sweater. It’s excitement all the way! 7346ch00FM.qxd 11/20/06 2:48 PM Page viii ix About the Technical Reviewers ■NICK MCCOLLUM has more than 18 years of experience designing and developing enterprise applications on a variety of platforms. He is a principal consultant for NuSoft Solutions, Inc., and is currently the architect and lead developer for http://www.spout.com. Nick has acted as a technical editor for the following publications: C# COM+ Programming by Derek Beyer (John Wiley & Sons, 2001) and Pro Ajax and the .NET 2.0 Platform by Daniel Woolston (Apress, 2006). He is a Microsoft Certified Solution Developer and frequently speaks at Microsoft events and local user group meetings in the West Michigan area. ■BERNHARD SEEFELD cofounded the Swiss search engine http://search.ch in 1995, where in October 2004, he released the first worldwide Ajax mapping application, http://map.search.ch. Bernhard holds an MSc in theoretical physics from the University of Berne, Switzerland. 7346ch00FM.qxd 11/20/06 2:48 PM Page ix [...]... is a way of organizing how to present data that the Ajax application can manipulate Let’s go back to the calculator example and go through a preliminary version of converting the application into Ajax and REST Figure 1-4 shows the Ajax- enabled sample application 11 7346ch01.qxd 12 11/17/06 10:31 AM Page 12 CHAPTER 1 ■ GETTING STARTED Figure 1-4 Ajax- enabled calculator application The Ajax- enabled calculator... more akin to a database, and SOAP is more akin to an API This is understandable because of the way each approach exposes its URL, and the data that is sent and received The semantics of what the data is and represents at the URL is very different for SOAP and REST In a REST approach, if you use HTTP PUT to save data on the server, then you assume to get the same data when calling HTTP GET In a SOAP approach, ... references to a resource and not necessarily a file on a hard disk 1-1 The Easiest Way to Get Started with Ajax and REST Problem You want to know the best way to get started with writing Ajax and REST Solution When developing an Ajax and REST application, you must decide on the tools and frameworks you’ll use The choice is simple: Use whatever you’re using today, and write some Ajax applications You don’t... to illustrate that REST and SOAP are very different in their approaches All of this theory sounds like hand waving, so the best way to explain the theory is to implement a service using REST The example calculator application starts with a traditional API approach, converts the application into a preliminary REST approach, and then transforms the preliminary solution into a full-fledged REST solution. .. they can pay less upfront and more later, or pay a large amount upfront, nothing for a while, and then the rest later In these examples, the math used to calculate the mortgage is identical What changes is how the calculations are assembled You can use a constrained language for all problems, and you can use a duck-typed language for all problems You can also mix and match using Web services or compatibility... practical solutions for implementing Ajax, JavaScript, and Representational State Transfer (REST) –based Web services and functionality I want to promote the development of applications that are decoupled (client code is separate from the server code), applications that can be adequately tested and maintained, and code that can be used by clients outside of the Ajax context I believe in Ajax, but I also... routines Problem You want to know the best way to test your Ajax and REST applications Solution This recipe explains the different layers of test-driven development techniques.10 There are different layers because an Ajax and REST application involves both client-side and server-side code To put it simply, you don’t hire a few users to test an Ajax and REST application and get them to try out application... may not be the same When trying to understand the definition and philosophy of Ajax, remember the following points: • The biggest advantage to Ajax and its associated technologies is its ability to act in a dynamic fashion using duck typing • Using Ajax, you need to stop thinking of data and the GUI as static elements The data and GUI can and should be extended at runtime, allowing you to combine and. .. compatibility layers When you’re writing JavaScript and Ajax code, you’re writing duck-typed code It’s important to realize that Ajax is not just about XML, JavaScript, and DHTML It’s about the ability to dynamically create content and code that can be injected and executed This ability to create content and code dynamically is possible in a constrained language, but is incredibly difficult and tedious... client and server, the tests have to be a finer granularity than the contracts The implementation tests need to be extensive and go beyond the contract and may include other aspects such as data initialization and presentation For example, the test layers 1 and 3, as illustrated in Figure 1-9 , are not related directly to the contracts and are used to test the client- and server-side implementations Starting . Ajax and Java Frameworks Beginning Ajax with PHP Foundations of Ajax Pro Ajax and the .NET 2.0 Platform Ajax and REST Recipes Ajax Patterns and Best Practices Ajax and REST Recipes A Problem-Solution. & color not accurate spine = 0.838" 360 page count BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® Ajax and REST Recipes: A Problem-Solution Approach Dear Reader, Ajax and REST Recipes: A. code), applications that can be adequately tested and maintained, and code that can be used by clients outside of the Ajax context. I believe in Ajax, but I also believe that if you can develop Ajax

Ngày đăng: 27/03/2014, 13:34

Từ khóa liên quan

Mục lục

  • Ajax and REST Recipes: A Problem-Solution Approach

    • Contents

    • Introduction

    • Chapter 1 Getting Started

    • Chapter 2 JavaScript Recipes

    • Chapter 3 Dynamic Content Recipes

    • Chapter 4 Implementing an SOA Architecture

    • Chapter 5 Implementing a Universal Web Service Architecture

    • Chapter 6 Implementing Web Services for Large or Slow Data Sets

    • Chapter 7 Implementing an Ajax Shopping Cart

    • Chapter 8 Don’t Submit Your Forms—Ajax Them

    • Index

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan