SlideShare ist ein Scribd-Unternehmen logo
1 von 56
Downloaden Sie, um offline zu lesen
Merb with jQuery
(not RJS)
Rails and JS
<% remote_form_for(@user) do |f| %>
  <%= f.text_field :username %>
<% end %>
<form action=quot;/usersquot; class=quot;new_userquot;
id=quot;new_userquot; method=quot;postquot; onsubmit=quot;new
Ajax.Request('/users', {asynchronous:true,
evalScripts:true,
parameters:Form.serialize(this)}); return
false;quot;><div style=quot;margin:0;padding:
0quot;><input name=quot;authenticity_tokenquot;
type=quot;hiddenquot;
               inline JS
value=quot;dcab4e1ab6a78f1f618b97008246df33340
75ca9quot; /></div>
  <input id=quot;user_usernamequot;
name=quot;user[username]quot; size=quot;30quot;
type=quot;textquot; />
</form>
<center>
  <font face=quot;Arialquot;>Hello</font>
</center>
<center>
  <font face=quot;Arialquot;>Hello</font>
</center>
              we stopped
              doing this
“It works”
Kinda.
Difficulty




           Time in Project
Difficulty remote_form_for...
      fantastic! so easy!



               Time in Project
Difficulty  autocomplete!
         wow! I don’t need
             any JS!


               Time in Project
holy cow... RJS...
Difficulty
       Who needs this stodgy
            “Javascript”



              Time in Project
Difficulty

           one day...



             Time in Project
we need an
Difficulty
    autocompleter that talks
         to a web service



              Time in Project
Difficulty   quick... cancel
            the launch



                Time in Project
Difficulty




           Time in Project
Or...
Difficulty




           Time in Project
learn JS or
    hire someone who
Difficulty knows JS




             Time in Project
“It’s hard”
Let’s take a look
<%= form_for(@user, :class => quot;remotequot;, :action => quot;/fooquot;) do %>
  <%= text_field :username %>
<% end =%>
<form class=quot;remotequot; method=quot;postquot; action=quot;/fooquot;>
  <input type=quot;textquot; class=quot;textquot; value=quot;quot; name=quot;usernamequot; id=quot;params_usernamequot;/>
</form>
And the JavaScript
(scary!)
$(quot;form.remotequot;).ajaxForm();
link_to_remote
<%= link_to_remote quot;Awesome!quot;,
    :url => quot;/fooquot;,
    :update => quot;some_idquot; %>
<a href=quot;#quot; onclick=quot;new
Ajax.Updater('some_id', '/foo',
{asynchronous:true, evalScripts:true,
parameters:'authenticity_token=' +
encodeURIComponent('dcab4e1ab6a78f1f618b97
008246df3334075ca9')}); return
false;quot;>Awesome!</a> JS!
              inline
<a href=quot;#quot; onclick=quot;new
Ajax.Updater('some_id', '/foo',
{asynchronous:true, evalScripts:true,
parameters:'authenticity_token=' +
encodeURIComponent('dcab4e1ab6a78f1f618b97
008246df3334075ca9')}); return
              (so easy!)
false;quot;>Awesome!</a>
<%= link_to quot;Awesome!quot;, quot;/fooquot;,
    :rel => quot;#some_idquot;,
    :class => quot;remotequot; %>
<a href=quot;/fooquot; class=quot;remotequot; rel=quot;#some_idquot;>Awesome!</a>
“I bet the JavaScript is super-crazy”
$(quot;a.remotequot;).click(function() {
   if(this.rel) $(this.rel).load(this.href);
   else $.get(this.href);
   return false;
})
CSS selector


$(quot;a.remotequot;).click(function() {
   if(this.rel) $(this.rel).load(this.href);
   else $.get(this.href);
   return false;
})
$(quot;a.remotequot;).click(function() {
   if(this.rel) $(this.rel).load(this.href);
   else $.get(this.href);
   return false;
})

     supplying a rel
     means update
$(quot;a.remotequot;).click(function() {
   if(this.rel) $(this.rel).load(this.href);
   else $.get(this.href);
   return false;
})
    otherwise
    use $.get
X
$(quot;a.remotequot;).click(function() {



})
   if(this.rel) $(this.rel).load(this.href);
   else $.get(this.href);
   return false;




            no JS?
<a href=quot;/fooquot; class=quot;remotequot; rel=quot;#some_idquot;>Awesome!</a>
Want a helper?
def link_to_remote(text, url, options = {})
  if options.key?(:update)
    options[:rel] = options.delete(:update)
  end

  options[:class] = options[:class].to_s.
    split(quot; quot;).push(quot;remotequot;).join(quot; quot;)

  link_to(text, url, options)
end
If you switch to Prototype (o.O)
$$(quot;a.remotequot;).observe(quot;clickquot;, function() {
  if(this.rel) new Ajax.Updater($$(this.rel), this.href);
  else new Ajax.Request(this.href);
  return false;
});
Using JSON
class Speakers < Application
  provides :json

  def index
    @speakers = Speaker.all
    display @speakers
  end

end
class Speakers < Application
  provides :json

  def index
    @speakers = Speaker.all
    display @speakers
  end

end
$.getJSON(quot;/speakersquot;, function(json) {
  // [{id: 1, name: quot;Joe Blowquot;}]
});
$.getJSON(quot;/speakersquot;, function(json) {
  // [{id: 1, name: quot;Joe Blowquot;}]
});
      Accept:
 application/json
class Speakers < Application
  provides :json

  def index
    @speakers = Speaker.all
    display @speakers
  end

end    @speakers.to_json
What about RJS?
page.insert_html :bottom, 'list',
                 content_tag(quot;liquot;, quot;Foxquot;)
Generated JS
try {
  new Insertion.Bottom(quot;listquot;, quot;<li>Fox</
li>quot;);
} catch (e) {
  alert('RJS error:nn' + e.toString());
alert('new Insertion.Bottom(quot;listquot;,
quot;<li>Fox</li>quot;);');
  throw e
}
Big Scary JS
$.get(quot;/ajax_urlquot;, function(html) {
   $(quot;#listquot;).append(html)
})
Thank you.

Weitere ähnliche Inhalte

Was ist angesagt?

WordPress Admin UI - Future Proofing Your Admin Pages
WordPress Admin UI - Future Proofing Your Admin PagesWordPress Admin UI - Future Proofing Your Admin Pages
WordPress Admin UI - Future Proofing Your Admin Pages
Brandon Dove
 

Was ist angesagt? (20)

jQuery Essentials
jQuery EssentialsjQuery Essentials
jQuery Essentials
 
22 j query1
22 j query122 j query1
22 j query1
 
WordPress Admin UI - Future Proofing Your Admin Pages
WordPress Admin UI - Future Proofing Your Admin PagesWordPress Admin UI - Future Proofing Your Admin Pages
WordPress Admin UI - Future Proofing Your Admin Pages
 
Devoxx 2014-webComponents
Devoxx 2014-webComponentsDevoxx 2014-webComponents
Devoxx 2014-webComponents
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
DOM Scripting Toolkit - jQuery
DOM Scripting Toolkit - jQueryDOM Scripting Toolkit - jQuery
DOM Scripting Toolkit - jQuery
 
JavaScript: Ajax & DOM Manipulation
JavaScript: Ajax & DOM ManipulationJavaScript: Ajax & DOM Manipulation
JavaScript: Ajax & DOM Manipulation
 
jQuery
jQueryjQuery
jQuery
 
Getting the Most Out of jQuery Widgets
Getting the Most Out of jQuery WidgetsGetting the Most Out of jQuery Widgets
Getting the Most Out of jQuery Widgets
 
KMUTNB - Internet Programming 4/7
KMUTNB - Internet Programming 4/7KMUTNB - Internet Programming 4/7
KMUTNB - Internet Programming 4/7
 
JQuery in Seaside
JQuery in SeasideJQuery in Seaside
JQuery in Seaside
 
Javascript: Ajax & DOM Manipulation v1.2
Javascript: Ajax & DOM Manipulation v1.2Javascript: Ajax & DOM Manipulation v1.2
Javascript: Ajax & DOM Manipulation v1.2
 
Prototype & jQuery
Prototype & jQueryPrototype & jQuery
Prototype & jQuery
 
Introduction to JQuery
Introduction to JQueryIntroduction to JQuery
Introduction to JQuery
 
Telling Stories With RSpec
Telling Stories With RSpecTelling Stories With RSpec
Telling Stories With RSpec
 
SharePoint and jQuery Essentials
SharePoint and jQuery EssentialsSharePoint and jQuery Essentials
SharePoint and jQuery Essentials
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
Introduction to Prototype JS Framework
Introduction to Prototype JS FrameworkIntroduction to Prototype JS Framework
Introduction to Prototype JS Framework
 
Write Less Do More
Write Less Do MoreWrite Less Do More
Write Less Do More
 
Web Projects: From Theory To Practice
Web Projects: From Theory To PracticeWeb Projects: From Theory To Practice
Web Projects: From Theory To Practice
 

Andere mochten auch

Merb Day Keynote
Merb Day KeynoteMerb Day Keynote
Merb Day Keynote
Yehuda Katz
 
Vaporware To Awesome
Vaporware To AwesomeVaporware To Awesome
Vaporware To Awesome
Yehuda Katz
 
Why You Shouldn't Write OO
Why You Shouldn't Write OO Why You Shouldn't Write OO
Why You Shouldn't Write OO
Yehuda Katz
 
Making your oss project more like rails
Making your oss project more like railsMaking your oss project more like rails
Making your oss project more like rails
Yehuda Katz
 

Andere mochten auch (6)

Merb Day Keynote
Merb Day KeynoteMerb Day Keynote
Merb Day Keynote
 
Appnovation One Sheet
Appnovation One SheetAppnovation One Sheet
Appnovation One Sheet
 
Vaporware To Awesome
Vaporware To AwesomeVaporware To Awesome
Vaporware To Awesome
 
Testing Merb
Testing MerbTesting Merb
Testing Merb
 
Why You Shouldn't Write OO
Why You Shouldn't Write OO Why You Shouldn't Write OO
Why You Shouldn't Write OO
 
Making your oss project more like rails
Making your oss project more like railsMaking your oss project more like rails
Making your oss project more like rails
 

Ähnlich wie Merb jQuery

Building Web Interface On Rails
Building Web Interface On RailsBuilding Web Interface On Rails
Building Web Interface On Rails
Wen-Tien Chang
 
Rails 3 And The Real Secret To High Productivity Presentation
Rails 3 And The Real Secret To High Productivity PresentationRails 3 And The Real Secret To High Productivity Presentation
Rails 3 And The Real Secret To High Productivity Presentation
railsconf
 
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)
Carles Farré
 
Html and i_phone_mobile-2
Html and i_phone_mobile-2Html and i_phone_mobile-2
Html and i_phone_mobile-2
tonvanbart
 
Solr and symfony in Harmony with SolrJs
Solr and symfony in Harmony with SolrJsSolr and symfony in Harmony with SolrJs
Solr and symfony in Harmony with SolrJs
Wildan Maulana
 

Ähnlich wie Merb jQuery (20)

Building Web Interface On Rails
Building Web Interface On RailsBuilding Web Interface On Rails
Building Web Interface On Rails
 
Rails 3 And The Real Secret To High Productivity Presentation
Rails 3 And The Real Secret To High Productivity PresentationRails 3 And The Real Secret To High Productivity Presentation
Rails 3 And The Real Secret To High Productivity Presentation
 
JavaScript on Rails 튜토리얼
JavaScript on Rails 튜토리얼JavaScript on Rails 튜토리얼
JavaScript on Rails 튜토리얼
 
Page Caching Resurrected
Page Caching ResurrectedPage Caching Resurrected
Page Caching Resurrected
 
Developing and testing ajax components
Developing and testing ajax componentsDeveloping and testing ajax components
Developing and testing ajax components
 
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)
 
Html and i_phone_mobile-2
Html and i_phone_mobile-2Html and i_phone_mobile-2
Html and i_phone_mobile-2
 
Solr and symfony in Harmony with SolrJs
Solr and symfony in Harmony with SolrJsSolr and symfony in Harmony with SolrJs
Solr and symfony in Harmony with SolrJs
 
Front End on Rails
Front End on RailsFront End on Rails
Front End on Rails
 
Plone Interactivity
Plone InteractivityPlone Interactivity
Plone Interactivity
 
Rugalytics | Ruby Manor Nov 2008
Rugalytics | Ruby Manor Nov 2008Rugalytics | Ruby Manor Nov 2008
Rugalytics | Ruby Manor Nov 2008
 
jQuery for Sharepoint Dev
jQuery for Sharepoint DevjQuery for Sharepoint Dev
jQuery for Sharepoint Dev
 
What's new in Rails 2?
What's new in Rails 2?What's new in Rails 2?
What's new in Rails 2?
 
Django - Framework web para perfeccionistas com prazos
Django - Framework web para perfeccionistas com prazosDjango - Framework web para perfeccionistas com prazos
Django - Framework web para perfeccionistas com prazos
 
Boston Computing Review - Ruby on Rails
Boston Computing Review - Ruby on RailsBoston Computing Review - Ruby on Rails
Boston Computing Review - Ruby on Rails
 
Jsf Ajax
Jsf AjaxJsf Ajax
Jsf Ajax
 
Struts2
Struts2Struts2
Struts2
 
Opensocial Codelab
Opensocial CodelabOpensocial Codelab
Opensocial Codelab
 
Javascript Basic
Javascript BasicJavascript Basic
Javascript Basic
 
Ajax On S2 Odp
Ajax On S2 OdpAjax On S2 Odp
Ajax On S2 Odp
 

Mehr von Yehuda Katz

Sprout core and performance
Sprout core and performanceSprout core and performance
Sprout core and performance
Yehuda Katz
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
Yehuda Katz
 
Organizing jQuery Projects Without OO
Organizing jQuery Projects Without OOOrganizing jQuery Projects Without OO
Organizing jQuery Projects Without OO
Yehuda Katz
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
Yehuda Katz
 

Mehr von Yehuda Katz (10)

Sprout core and performance
Sprout core and performanceSprout core and performance
Sprout core and performance
 
Writing Fast Client-Side Code: Lessons Learned from SproutCore
Writing Fast Client-Side Code: Lessons Learned from SproutCoreWriting Fast Client-Side Code: Lessons Learned from SproutCore
Writing Fast Client-Side Code: Lessons Learned from SproutCore
 
SproutCore: Amber
SproutCore: AmberSproutCore: Amber
SproutCore: Amber
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
 
Organizing jQuery Projects Without OO
Organizing jQuery Projects Without OOOrganizing jQuery Projects Without OO
Organizing jQuery Projects Without OO
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
 
Merb Camp Keynote
Merb Camp KeynoteMerb Camp Keynote
Merb Camp Keynote
 
Merb
MerbMerb
Merb
 
DataMapper
DataMapperDataMapper
DataMapper
 
jQuery and Ruby Web Frameworks
jQuery and Ruby Web FrameworksjQuery and Ruby Web Frameworks
jQuery and Ruby Web Frameworks
 

Kürzlich hochgeladen

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Kürzlich hochgeladen (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 

Merb jQuery