Top Banner
Rails and Ajax March 16, 2006
36

Rails and Ajax

Jan 19, 2015

Download

Documents

Sampetruda

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Rails and Ajax

Rails and AjaxMarch 16, 2006

Page 2: Rails and Ajax

Rails and Ajax

• Quick history of Ajax• Why Ajax? Why not?• Rails Ajax helpers• RJS templates• Graceful degradation• Ajax scaffolding

Page 3: Rails and Ajax

Rails and Ajax

• Quick history of Ajax• Why Ajax? Why not?• Rails Ajax helpers• RJS templates• Graceful degradation• Ajax scaffolding

Page 4: Rails and Ajax

Ye Olde Web: HTTP RequestGET /default.asp?query=fun+blinking+animated+gif+horizontal+rule

Page 5: Rails and Ajax

Ye Olde Web: HTTP Response

HTTP/1.0 200 OKDate: Fri, 31 Dec 1993 12:34:56 GMTContent-Type: text/htmlContent-Length: 954

<html><body><h1>Your search results!</h1>...

</body></html>

Page 6: Rails and Ajax

Full Page Refresh

• “Clunky” compared to desktop apps.• Unnecessary bandwidth.

Page 7: Rails and Ajax

Solution?

Page 8: Rails and Ajax

Innovation: Thank… Microsoft?• 1997 – Remote Scripting via Java applet.• 1999 – XMLHttpRequest object in IE5.• 2000 – Mozilla supports XHR.• 2004 – Safari supports XHR.• 2004 – Opera supports XHR.• 2005 – Jesse James Garret coins “Ajax.”

Reference: http://aspadvice.com/blogs/jeffc/archive/2005/10/26/13440.aspxCoining the Term: http://www.adaptivepath.com/publications/essays/archives/000385.php

Page 9: Rails and Ajax

Why “Ajax?”

• http://www.adaptivepath.com/publications/essays/archives/000385.php

• Asynchronous JavaScript and X(HT)ML

Page 10: Rails and Ajax

http://www.adaptivepath.com/images/publications/essays/ajax-fig1.png

© 2006 Adaptive Path, LLC

Page 11: Rails and Ajax

Rails and Ajax

• Quick history of Ajax• Why Ajax? Why not?• Rails Ajax helpers• RJS templates• Graceful degradation• Ajax scaffolding

Page 12: Rails and Ajax

Reasons for Ajax

• Quicker response.• Less bandwidth• Additional interactivity.

– Autocomplete fields.– Live search.– Upload progress bar.

• Most importantly…

Page 13: Rails and Ajax

Buzzword compliance!

http://headrush.typepad.com/photos/uncategorized/techbuzzwords_21.jpg

Page 14: Rails and Ajax

Reasons against Ajax

• Requires JavaScript.– Accessibility.

• Bookmarking problem.

Page 15: Rails and Ajax

Reasons against Ajax

• Both of these can be addressed.– Degrade gracefully.– Take care when choosing when to use Ajax.

Page 16: Rails and Ajax

Rails and Ajax

• Quick history of Ajax• Why Ajax? Why not?• Rails Ajax helpers• RJS templates• Graceful degradation• Ajax scaffolding

Page 17: Rails and Ajax

Rails Ajax Helpers

• link_to_remote• form_tag_remote• sortable_element

Page 18: Rails and Ajax

Ajax Helpers Example!

Page 19: Rails and Ajax

More Rails Ajax Helpers!

• observe_field• periodically_call_remote• auto_complete_field• remote_function

Page 20: Rails and Ajax

File Upload Progress

• http://sean.treadway.info/demo/upload/

Page 21: Rails and Ajax

Rails and Ajax

• Quick history of Ajax• Why Ajax? Why not?• Rails Ajax helpers• RJS templates• Graceful degradation• Ajax scaffolding

Page 22: Rails and Ajax

HTML Template

• RHTML– Embedding Ruby in HTML for views

Page 23: Rails and Ajax

XML Template

• RXML– Building XML with Ruby

xml.instruct! :xml, :version=>"1.0" xml.instruct! :rss, :version=>"2.0" xml.channel{

for hat in @hatsxml.hat do

xml.title(hat.name)xml.description(hat.description)

endend

}

Page 24: Rails and Ajax

JS Template

• RJS– Slated for Rails 1.1.– Currently available in Edge Rails.

• rake freeze_edge• rake update_javascripts

Page 25: Rails and Ajax

How does RJS work?

• Template is passed a page object.• Method calls are translated to JS.

Page 26: Rails and Ajax

RJS Example!

Page 27: Rails and Ajax

Inline RJSclass AdminController < ApplicationController

def update_usersrender :update do |page|

page.replace_html 'user_list',:partial => 'user',:collection => @users

page.visual_effect :highlight, 'user_list'end

endend

Page 28: Rails and Ajax

More RJS Examplespage.alert('Hello, world!')page.redirect_to(:action=>'user_signup')

page.assign('myVar', 42)page.call('doJavascriptMagic', 'paramXYZ', 23)

page.show('extra_help')page.hide('extraneous_information')page.toggle('user_detail')

Page 29: Rails and Ajax

Rails and Ajax

• Quick history of Ajax• Why Ajax? Why not?• Rails Ajax helpers• RJS templates• Graceful degradation• Ajax scaffolding

Page 30: Rails and Ajax

Graceful Degradation

• Accesibility / Usability.• RESTful APIs.

Page 31: Rails and Ajax

Graceful Degradation

<% if request.xhr? %>You arrived here via an XHR!Go nuts with it.

<% else %>You got here through astandard request.

<% end>

Page 32: Rails and Ajax

Graceful Degradation

def new# Creation code here

if request.xhr?render :partial => “item”

elseredirect_to :action => “list”

endend

Page 33: Rails and Ajax

Rails and Ajax

• Quick history of Ajax• Why Ajax? Why not?• Rails Ajax helpers• RJS templates• Graceful degradation• Ajax scaffolding

Page 34: Rails and Ajax

Ajax Scaffolding

• http://ajaxscaffold.height1percent.com/– gem install ajax_scaffold_generator– ruby script/generate ajax_scaffold Widget

Page 35: Rails and Ajax

Ajax Scaffolding Example!

Page 36: Rails and Ajax

Question and Answer!