Section 5.1. Instructions Instead of Data


5.1. Instructions Instead of Data

The Rails solution is to return JavaScript instructions, instead of HTML data, to Ajax requests. The JavaScript is executed as it comes in, so it can do anything that's possible from scriptinginsert new content into the page, create visual effects, call methods from external JavaScript librariesyou name it. And you can include as many JavaScript statements as you need in one response, so it's trivially easy to update a bunch of page elements at once. Try that with the standard link_to_remote :update => ... helper and you'll quickly appreciate how valuable JavaScript can be.

The power of that simple ideareturning JavaScript to Ajax requestscan't be overstated. Suddenly the server's role in Ajax applications has gone beyond just providing data; now it participates in the client-side logic as well. Of course, there's nothing Rails-specific about basic idea of returning JavaScript to Ajax requests; it could be implemented in any language or framework. What sets Rails apart from the rest is how the JavaScript is created.




Ajax on Rails
Ajax on Rails
ISBN: 0596527446
EAN: 2147483647
Year: 2006
Pages: 103
Authors: Scott Raymond

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net