from the very earliest versions. Prototype and Ajaxy helpers have traditionally been “just there” and have always “just worked” Tuesday, February 1, 2011
from the very earliest versions. Prototype and Ajaxy helpers have traditionally been “just there” and have always “just worked” Magic Tuesday, February 1, 2011
your behavior-defining script out of your content. It’s the same idea as keeping your style- defining rules (CSS) out of your content. Tuesday, February 1, 2011
the past, if you wanted to use unobtrusive javascript techniques in Rails, you simply stopped using the built-in Javascript helpers... Tuesday, February 1, 2011
the Javascript helpers: 2. No more ‘remote’ helpers (link_to_remote, remote_form_for, et al., are gone). 1. Instead of inserting inline JS, Rails 3 uses HTML5 data- attributes. Tuesday, February 1, 2011
the Javascript helpers: 3. It’s a lot easier to pull out Prototype and switch to jQuery or mootools, etc, if you want to. 2. No more ‘remote’ helpers (link_to_remote, remote_form_for, et al., are gone). 1. Instead of inserting inline JS, Rails 3 uses HTML5 data- attributes. Tuesday, February 1, 2011
of a new feature of HTML5: any tag can have any number of attributes prepended with “data-”, and still be perfectly valid markup. Tuesday, February 1, 2011
else) are handled from /public/javascripts/rails.js // from the jQuery version of rails.js: function allowAction(element) { var message = element.attr('data-confirm'); return !message || (fire(element, 'confirm') && confirm (message)); } Tuesday, February 1, 2011
as: <a href="/posts/1" data-confirm="Are you sure?" data-method="delete" data-remote="true" rel="nofollow">Delete</a> And clicking on it will fire a function in rails.js which creates an XMLHttpRequest and sends it to the URL in your href attribute. Tuesday, February 1, 2011
for example, we simply add to our Gemfile: gem 'jquery-rails' And then (in the terminal) : % bundle install % rails g jquery:install Tuesday, February 1, 2011
RJS, just adding jquery-rails does not (at the moment, at least) provide jQuery equivalents for RJS statements. You may be able to use jrails, which does (or at least did) provide these. (I haven’t tried this.) Otherwise, you’ll want to stick with Prototype. Tuesday, February 1, 2011
fired with each xhr (they differ slightly between the Prototype and jQuery version of rails.js): // Prototype rails.js ajax:before ajax:success ajax:complete ajax:failure ajax:after // jQuery rails.js ajax:beforeSend ajax:success ajax:complete ajax:error Tuesday, February 1, 2011
view helpers; Rails 3 continues this. Rails 3 js changes include: 1. Utilizing HTML5 data-* attributes. 2. Adding a :remote param to helpers that can use it. 3. Making it easy to drop in alternate JS frameworks in place of Prototypejs. However, in Rails 3, these helpers aim to be unobtrusive. Tuesday, February 1, 2011
view helpers; Rails 3 continues this. You can either use a js template to send js right back to the browser, or you can take advantage of the ajax hooks provided by rails.js. Rails 3 js changes include: 1. Utilizing HTML5 data-* attributes. 2. Adding a :remote param to helpers that can use it. 3. Making it easy to drop in alternate JS frameworks in place of Prototypejs. However, in Rails 3, these helpers aim to be unobtrusive. Tuesday, February 1, 2011
Ryan Bigg (@ryanbigg), will contain some examples of UJS. It’s not complete yet, but if you’re interested in checking it out (http://manning.com/katz/), there is a discount code (50% off; valid Jan 23 to Feb 23): rails350 Tuesday, February 1, 2011