23
jQuery & AJAX with Rails Alan Hecht http://alanhecht.me

jQuery and AJAX with Rails

Embed Size (px)

DESCRIPTION

ATLRUG Emerald City Presentation - 3/26/2011

Citation preview

Page 1: jQuery and AJAX with Rails

jQuery & AJAXwith Rails

Alan Hechthttp://alanhecht.me

Page 2: jQuery and AJAX with Rails

Additional FirefoxDebugging Tools

• HttpFox- Analyze incoming & outgoing HTTP traffic- Can filter on strings

• Visual Event- JavaScript bookmarklet for examining JavaScript &

jQuery events on a page- www.sprymedia.co.uk/article/Visual+Event- Copy to bookmark toolbar- Click on ‘Visual Event’ bookmark to show events

Page 3: jQuery and AJAX with Rails

Visual Event & HttpFoxDemo

Page 4: jQuery and AJAX with Rails

Options for Using jQuery with Rails

• Manually add jQuery to Rails

• Install and run the jQuery Rails adapter gem

Page 5: jQuery and AJAX with Rails

Manual jQuery Installation

• Add a copy of jQuery to ‘public/javascripts/

• Edit ‘config/application.rb’ to automatically load jQuery with each page- Add the line:

- Can always include jQuery with specific pages

Page 6: jQuery and AJAX with Rails

jQuery Scripts

• public/javascripts/application.js is loaded with every page

• All other JavaScript files must be explicitly loaded unless added JavaScript default pages in ‘config/application.rb’

• JavaScript to be executed will be a function wrapped in the jQuery

$(document).ready( ) event

Page 7: jQuery and AJAX with Rails

jQuery w/ RailsInstallation Demo

Page 8: jQuery and AJAX with Rails

Key Conceptsfor

Working with jQuery

• Unobtrusive JavaScript

• AJAX

Page 9: jQuery and AJAX with Rails

Unobtrusive JavaScript

• Separation of JavaScript and HTML- No in-line event handlers- All JavaScript code in external files

• Your site works with or without JavaScript

• HTML should be just as accessible to those with disabilities or devices that don’t support JavaScript

Page 10: jQuery and AJAX with Rails

Unobtrusive JavaScriptIssues

• Need to design site for use with or without JavaScript

• Doesn’t work with sites where JavaScript is integral to proper function

• Biggest benefit is for designing sites for people with disabilities- Mobile devices starting to have some form

of Javascript support

Page 11: jQuery and AJAX with Rails

AJAX

• Asynchronous JavaScript and XML

• Can call the web server without refreshing the entire page

• Implemented with XMLHttpRequest- Send data directly to web server- Response loaded directly into script- JSON commonly used instead of XML

Page 12: jQuery and AJAX with Rails

jQuery Rails Adapter

• Supports unobtrusive JavaScript for jQuery with Rails

• Adds AJAX support- Not necessary for AJAX but provides

helpers- Site works both with & without AJAX

• Will be installed by default in Rails 3.1

Page 13: jQuery and AJAX with Rails

jQuery Adapter Installation

• Add ‘jquery-rails’ to Gemfile- bundle install

• Set up Rails project to work with jQuery- rails generate jquery:install- say ‘yes’ to overwrite rails.js

• Can skip Prototype installation in new Rails app- rails new <application> --skip-prototype

Page 14: jQuery and AJAX with Rails

AJAX Using jQuery Adapter

• Implemented with unobtrusive JavaScript using custom attributes:

- data-method: HTTP method to call (PUT, GET, DELETE, or POST)

- data-confirm: confirmation dialog before proceding

- data-remote: submit via AJAX if true

- data-disable-with: disable form elements during submission

Page 15: jQuery and AJAX with Rails

HTML Changes

• Add ‘:remote => true’ to link or submit button to execute using AJAX

is rendered as:

Page 16: jQuery and AJAX with Rails

Controller Changes

• Need to respond to JavaScript in a similar manner to HTML

• JavaScript will be sent to client to be executed

Page 17: jQuery and AJAX with Rails

View Changes

• View file will contain JavaScript to be executed on the client

• File will be similarly named as the HTML file (i.e. create.js.erb or destroy.js.erb)

Page 18: jQuery and AJAX with Rails

AJAX w/ jQuery Adapter Demo

Page 19: jQuery and AJAX with Rails

AJAX w/ jQuery

• AJAX support built into jQuery- Good when a lot of functionality is on the client- Sites like Facebook limit server requests, so

calls need to be made from client- Does not support unobtrusive JavaScript

Page 20: jQuery and AJAX with Rails

$.ajax

• The jQuery function upon which all AJAX calls are built

• $.ajax(<url>, <settings>)url: the URL to callsettings: JSON object containing settings such as:

- HTTP call- function to call if successful

Page 21: jQuery and AJAX with Rails

$.ajax Example

Page 22: jQuery and AJAX with Rails

Additional AJAX Methods

• $.post(<url>, <data>, <success function>)

• $.get(<url>, <data>, <success function>)

Page 23: jQuery and AJAX with Rails

jQuery AJAX Demo