Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Practical prototype and scipt.aculo.us part 16: The information in this book is distributed on an "as is" basis, without warranty Although every pre-caution 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. | 78 CHAPTER 4 AJAX ADVANCED CLIENT SERVER COMMUNICATION Mocking Think of the client and server as humans. The JavaScript we write deals only with the browser to make an Ajax call our code has to ask the browser to make an external request and hand over the response when it s ready. Imagine Alice walking into a room and telling Bob to make a phone call for her. Bob calls Carol has a quick conversation and then hangs up and tells Alice what Carol said. But Alice has no direct contact with Carol. If Bob wanted to he could turn around pick up the phone pretend to dial a number pretend to have a conversation hang up and tell Alice whatever he feels like. The whole thing s a ruse but Alice is none the wiser. In the real world this would be dishonest and unwise behavior. In the computer world it helps us build our app faster. Lying to your code shouldn t make you feel bad. The code we write will make a request to a certain URL every 30 seconds it will expect a JSON response that follows a certain format. But it doesn t need to know how those stats are retrieved that s the job of the server. For testing purposes we need a stream of data that behaves the way a real stream would during game day games start out with no score but points are amassed over time as real-life players do good things. So let s write a script that will generate some mock stats for us. This script can be told which stats to report at any given time. It can behave the way real stats would. We ll be using PHI but the basic idea is the same for any language. Here s how it will work 1. We define several classes one or two for each position. These will represent the players. Each will score at a different rate. 2. We ll have stats running on a 10-minute cycle after which every score will reset. This may seem like a short cycle and indeed it s much shorter than a real football game but a shorter cycle only helps us develop more quickly. 3. We ll tell each class how to report its own score based on the current