SlideShare ist ein Scribd-Unternehmen logo
Unobtrusive
     JavaScript
     in Rails 3
           Hussein Morsy

             23.02.2010
Heinrich-Heine-Universität Düsseldorf
    Rails User Group Düsseldorf
http://twitter.com/
  HusseinMorsy
Wer verwendet Ajax in Rails?
Wer verwendet jQuery ?
Ereignisse 2009
XHTML 2 ist tot
Es lebe HTML 5
RailsConf 2009




Unobtrusive JavaScript mit
 HTML 5 data-Attribut
Grundlagen
HTML


• Enthält Daten
• Definiert die Struktur
CSS

• Design
• Layout
• Trennung von Design und Inhalt
JavaScript

• Interaktivität
• Animation
• Server-kommunikation mit Ajax
Unobtrusive JS

• Unobtrusive = unauffällig
• Kein JavaScript im HTML-Code
• Trennung von Inhalt und Verhalten
Progressive
        Enhancement

• Applikation funktioniert auch ohne JS
• Erst die App ohne Ajax entwickeln
• Dann erst die Ajax Funktionalität
  hinzufügen
HTML 5


• Nachfolger von HTML 4
• Google, Apple, etc. verwenden jetzt schon
  HTML 5
Valides HTML 5
<!DOCTYPE html>
<title>Hello World</title>
<p>Hallo</p>



                             Validator
HTML-Data-Attribute
• Speichern von Zusatzinformationen, wie z.B.
• <a href="/bookmarks/4" data-remote="true">
  Show detail
  </a>
• <a href="/obst" data-nur-ein-test="Apfel, Banane">
  Obst Liste
  </a>
Ajax in Rails 2
Ajax Helper in Rails 2
• link_to_remote
• link_to_function
• remote_form_for                 Auch in Rails 3
                           mit Prototype legacy Helper
• form_remote_tag             http://github.com/rails/
                            prototype_legacy_helper
• button_to_remote
• submit_to_remote
• periodically_call_remote
• observe_field und observe_for
link_to_remote

<%= link_to_remote 'show', @boomark %>




<a href="#" onclick="new Ajax.Request('/bookmarks/117',
{asynchronous:true, evalScripts:true,
parameters:'authenticity_token= ....
Ajax in Rails 3
link_to

<%= link_to 'Show', @bookmark, :remote => true %>




<a href="/bookmarks/117" data-remote="true">Show</a>
link_to zum Löschen

<%= link_to 'destroy', @bookmark, :method => :delete %>




<a href="/bookmarks/117" data-method="delete"
   rel="nofollow">Show</a>
form_for
<% form_for(@boomark, :remote => true) do |f| %>
  ...
<% end %>




<form action="/bookmarks" class="new_bookmark"
data-remote="true" id="new_bookmark" method="post">
submit

<%= f.submit :disable_with => "Please wait..." %>




<input data-disable-with="Please wait..."
id="post_submit" name="commit" type="submit" value="Create
Bookmark" />
public/javascripts/rails.js


• Enthält den JavaScript-Code für Ajax
• Default ist Prototype
Verwendung von
         jQuery
• rails projektname --skip-prototype oder
• rails projektname -J
• jQuery rails.js downloaden von
  http://github.com/rails/jquery-ujs
• kopieren nach
  public/javascripts/rails.js
Danke


twitter.com/HusseinMorsy

Weitere ähnliche Inhalte

KEY
Ruby on Rails SS09 11
PPT
Georges Vanier Elementary Hallway Displays. Oct '14
PPT
Kendall7e ch05
KEY
Content Analysis
PDF
Rubymotion trip to inspect 2013
PDF
Rack-Middleware
PDF
Homebrew
PDF
Datamapper Railskonferenz 2009
Ruby on Rails SS09 11
Georges Vanier Elementary Hallway Displays. Oct '14
Kendall7e ch05
Content Analysis
Rubymotion trip to inspect 2013
Rack-Middleware
Homebrew
Datamapper Railskonferenz 2009
Anzeige

Unobtrusive JavaScript in Rails 3