Danh mục

Test Driven JavaScript Development- P16

Số trang: 20      Loại file: pdf      Dung lượng: 191.69 KB      Lượt xem: 19      Lượt tải: 0    
tailieu_vip

Hỗ trợ phí lưu trữ khi tải xuống: 19,000 VND Tải xuống file đầy đủ (20 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Test Driven JavaScript Development- P16:This book is about programming JavaScript for the real world, using the techniquesand workflow suggested by Test-Driven Development. It is about gaining confidencein your code through test coverage, and gaining the ability to fearlessly refactor andorganically evolve your code base. It is about writing modular and testable code. Itis about writing JavaScript that works in a wide variety of environments and thatdoesn’t get in your user’s way.
Nội dung trích xuất từ tài liệu:
Test Driven JavaScript Development- P16 Streaming Data with Ajax and Comet 13 I n Chapter 12, Abstracting Browser Differences: Ajax, we saw how the XML- HttpRequest object enables web pages to take the role of interactive applications that can both update data on the back-end server by issuing POST requests, as well as incrementally update the page without reloading it using GET requests. In this chapter we will take a look at technologies used to implement live data streaming between the server and client. This concept was first enabled by Netscape’s Server Push in 1995, and is possible to implement in a variety of ways in today’s browsers under umbrella terms such as Comet, Reverse Ajax, and Ajax Push. We will look into two implementations in this chapter; regular polling and so-called long polling. This chapter will add some features to the tddjs.ajax.request interface developed in the previous chapter, add a new interface, and finally integrate with tddjs.util.observable, developed in Chapter 11, The Observer Pattern, enabling us to create a streaming data client that allows JavaScript objects to observe server-side events. The goal of this exercise is twofold: learning more about models for client- server interaction, and of course test-driven development. Important TDD lessons in this chapter includes delving deeper into testing asynchronous interfaces and testing timers. We will continue our discussion of stubbing, and get a glimpse of the workflow and choices presented to us as we develop more than a single interface. 293Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com 294 Streaming Data with Ajax and Comet 13.1 Polling for Data Although one-off requests to the server can enable highly dynamic and interesting applications, it doesn’t open up for real live applications. Applications such as Facebook’s and GTalk’s in-browser chats are examples of applications that cannot make sense without a constant data stream. Other features, such as stock tickers, auctions, and Twitter’s web interface become significantly more useful with a live data stream. The simplest way to keep a constant data stream to the client is to poll the server on some fixed interval. Polling is as simple as issuing a new request every so many milliseconds. The shorter delay between requests, the more live the applica- tion. We will discuss some ups and downs with polling later, but in order for that discussion to be code-driven we will jump right into test driving development of a poller. 13.1.1 Project Layout As usual we will use JsTestDriver to run tests. The initial project layout can be seen in Listing 13.1 and is available for download from the book’s website.1 Listing 13.1 Directory layout for the poller project chris@laptop:~/projects/poller $ tree . |-- jsTestDriver.conf |-- lib | `-- ajax.js | `-- fake_xhr.js | `-- function.js | `-- object.js | `-- stub.js | `-- tdd.js | `-- url_params.js |-- src | `-- poller.js | `-- request.js `-- test `-- poller_test.js `-- request_test.js 1. http://tddjs.comPlease purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com 13.1 Polling for Data 295 In many ways this project is a continuation of the previous one. Most files can be recognized from the previous chapter. The request.js file, and its test case are brought along for further development, and we will add some functionality to them. Note that the final refactoring discussed in Chapter 12, Abstracting Browser Differ- ences: Ajax, in which tdd.ajax.request returns an object representing the re- quest, is not implemented. Doing so would probably be a good idea, but we’ll try not to tie the two interfaces too tightly together, allowing the refactoring to be performed at some later time. Sticking with the code exactly as we developed it in the previous chapter will avoid any surprises, allowing us to focus entirely on new features. The jsTestDriver.conf configuration file needs a slight tweak for this project. The lib directory now contains an ajax.js file that depends on the tddjs object defined in tdd.js; however, it will be loaded before the file it depends on. The solution is to manually specify the tdd.js file first, then load the remaining lib files, as seen in Listing 13.2. Listing 13.2 Ensuring correct load order of test files server: http://localhost:4224 load: - lib/tdd.js - lib/stub.js - lib/*.js - src/*.js - test/*.js 13.1.2 The Poller: tddjs.ajax.poller In Chapter 12, Abstract ...

Tài liệu được xem nhiều: