Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Rails đã xây dựng hỗ trợ cho các cuộc gọi AJAX, mà làm cho nó rất dễ dàng để đặt ứng dụng của bạn theo dõi với Web, phiên bản 2.0. Trước hết, nó có các mẫu thử nghiệm, 4 hiệu ứng, DragDrop, và kiểm soát các thư viện JavaScript được xây dựng trong. Những thư viện gọn gàng bọc tất cả các loại hữu ích AJAX và các công cụ thao tác trên DOM trong một tốt đẹp, | The Rails Way 376 XMLHttpRequest vs. iframe So you ask what s all the hype about I did this with iframe for years While it s true you can do something along the lines of what XMLHttpRequest does iframes are not nearly as flexible nor as clean as AJAX to use. Unlike the iframe approach with AJAX it s easy to do GET POST and other HTTP request types the DOM is not altered in any way you have powerful callback hooks there s a clean API and you can customize HTTP headers. Considering all this it s obvious that XMLHttpRequest provides a far cleaner and more powerful programming model than that of iframes. - 18.2 The Rails Way Rails has built-in support for AJAX calls which makes it very easy to put your application on track with the Web version 2.0. First of all it has the prototype 4effects dragdrop and controls JavaScript prototype libraries built-in. These library neatly wrap all sorts of useful AJAX and DOM manipulation stuff in a nice object-oriented way. The second thing is JavascriptHelper a module that defines the methods JavascriptHelper we ll be looking at in the rest of this chapter. It wraps JavaScript access in pristine Ruby code so you won t have to switch to another language when using AJAX. Talk about total integration. To use any of the functions defined by JavascriptHelper you first have to include the prototype.js file in your application. Do this by making this call in the head section of your .rhtml page. javascript_include_tag prototype For the code in this chapter we ve added the call to javascript_include_tag to our overall application.rhtml layout file making the library available to all of our examples. 4 http prototype.conio.net Report erratum The Rails Way 377 You also need the prototype.js file in your application s public javascripts directory. It s included by default if you generate your application s structure by running the rails command. link_to_remote The syntax for making a basic AJAX call from an .rhtml template can be as simple as