Section 8.0. Introduction


8.0. Introduction

JavaScript is a prototype-based scripting language with syntax that's loosely based on the C programming language. It's closely related to ECMAScript, which is standardized by Ecma International as outlined in the ECMA-262 specification. In web applications, JavaScript is used to add dynamic functionality to static pages or to lighten the load on server-side processing by letting the user's browser do some of the work.

JavaScript's widespread adoption has always been dependant on how various web browsers have chosen to implement (or in some cases, ignore) various features of the language. JavaScript developers who have been around for a while will remember looking at browser compliance charts when deciding whether or not adding some JavaScript dynamism would sacrifice the portability of their web application. Luckily this situation has changed for the better; for whatever reason, browser vendors are no longer trying to gain market share by designing quirks into their software. Developers can now use JavaScript liberally in their web applications and be confident that most users will experience these features consistently.

There are still differences in the way the major browsers deal with specific JavaScript implementations, but fortunately there is a solution. A number of JavaScript helper libraries have emerged during the past few years that take the pain out of tasks such as browser version detection and compliance checks. These libraries also add a multitude of helper functions that make things like manipulating the DOM of a page much less verbose.

The JavaScript framework that Rails uses to make things easier is called Prototype. (Note that this name is often confused with the prototype property of JavaScript objects.) The Prototype library simplifies a number of common tasks in JavaScript, such as DOM manipulation and Ajax interaction. Complementing the Prototype framework, Rails also comes with the script.aculo.us JavaScript effects library. script.aculo.us has allowed web applications to used stunning effects that used to be associated only with desktop software. It's worth noting that Sam Stephenson (the creator of the Prototype framework) and Thomas Fuchs (the creator of script.aculo.us) are both on the Rails core team. This helps explain why both of these libraries are so nicely integrated into the Rails framework.

The real power of dealing with JavaScript and Ajax in Rails is that the framework makes doing so easy; so easy, that it's often no harder to add advanced dynamic features than it is to add any other HTML element to a page. The JavaScript helpers included with Rails and the RJS templating system allow you to avoid worrying about JavaScript code at all (unless you want to). What's really cool is that Rails lets you deal with JavaScript using Ruby code. This lets you stay in the mindframe of a single language during development, and often makes your code easier to understand and maintain down the line.

This chapter will show you some of the common effects you can achieve using JavaScript and Ajax from within the Rails framework. Hopefully, the ease with which you can add these features will encourage you to imagine and innovate new ways to apply these effects to your own Rails applications.




Rails Cookbook
Rails Cookbook (Cookbooks (OReilly))
ISBN: 0596527314
EAN: 2147483647
Year: 2007
Pages: 250
Authors: Rob Orsini

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