jquery and rails, sitting in a tree

47
Adam McCrea [email protected] @adamlogic

Upload: adamlogic

Post on 05-Dec-2014

10.618 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 2: jQuery and Rails, Sitting in a Tree

Intro to jQuery

jQuery with Rails 3

jQuery with Rails 2

Beyond the Rails (Ajax) Way

AGENDA

Page 3: jQuery and Rails, Sitting in a Tree

1. Select some HTML elements.

2. Call methods on them.

3. Repeat.

JQUERY RECIPE

$("p.neat").addClass("ohmy").show("slow");

Page 4: jQuery and Rails, Sitting in a Tree
Page 5: jQuery and Rails, Sitting in a Tree

$() == jQuery()

Page 6: jQuery and Rails, Sitting in a Tree

CSS Selector

$("p.neat") <p><p>

<p><p>

<p>

<p>

Page 7: jQuery and Rails, Sitting in a Tree

CSS Selector jQuery Wrapper

$("p.neat") <p><p>

<p><p>

<p>

<p>

Page 8: jQuery and Rails, Sitting in a Tree

$("p.neat").addClass("ohmy").show("slow");

jQuery Wrapper Methods

Page 9: jQuery and Rails, Sitting in a Tree
Page 10: jQuery and Rails, Sitting in a Tree

jQuery Wrapper

<p> <p><p>

<p>

Page 11: jQuery and Rails, Sitting in a Tree

jQuery function always returns jQuery wrapper

$(...) ALWAYS

Page 12: jQuery and Rails, Sitting in a Tree

$.fn.log = function() {  console.log(this);  return this;}

Write your own wrapper method

$("p.neat").log().show("slow");

Page 13: jQuery and Rails, Sitting in a Tree

Events

Page 14: jQuery and Rails, Sitting in a Tree

<a onclick="alert('I was clicked'); return false;">

html

Page 15: jQuery and Rails, Sitting in a Tree

$('a.clickme').bind('click', function(event) {  alert('I was clicked!'); event.preventDefault();});

<a class="clickme">

html

script

Page 16: jQuery and Rails, Sitting in a Tree

$('a.clickme').live('click', function(event) {  alert('I was clicked!'); event.preventDefault();});

<a class="clickme">

html

script

Page 17: jQuery and Rails, Sitting in a Tree

REVISITED

Page 18: jQuery and Rails, Sitting in a Tree

$(css_selector)

$(dom_element)

$(function)

$(html_string)

$()

Page 19: jQuery and Rails, Sitting in a Tree

$('a.clickme').live('click', function(event) {  alert('I was clicked!'); event.preventDefault();});

$(function() {

});

Page 20: jQuery and Rails, Sitting in a Tree

Intro to jQuery

jQuery with Rails 3

jQuery with Rails 2

Beyond the Rails (Ajax) Way

AGENDA

Page 21: jQuery and Rails, Sitting in a Tree

JQUERY + RAILS 3

Include jquery.js and rails.js

Include csrf_meta_tags

:remote => true

Render .js.erb templates

Page 22: jQuery and Rails, Sitting in a Tree
Page 23: jQuery and Rails, Sitting in a Tree

<!DOCTYPE html><html><head>  <title>Example</title>  <%= stylesheet_link_tag :all %>  <%= javascript_include_tag 'jquery', 'rails' %>  <%= csrf_meta_tag %></head>

<body>

<%= link_to 'view', item, :remote => true %>

</body></html>

Page 24: jQuery and Rails, Sitting in a Tree

<%= csrf_meta_tag %>

<%= link_to 'view', item, :remote => true %>

Page 25: jQuery and Rails, Sitting in a Tree

<meta name="csrf-param" content="authenticity_token"/><meta name="csrf-token" content="m3nej8PL1UVZt6ZOak1yugukEQ="/>

<%= csrf_meta_tag %>

<%= link_to 'view', item, :remote => true %>

Page 26: jQuery and Rails, Sitting in a Tree

<meta name="csrf-param" content="authenticity_token"/><meta name="csrf-token" content="m3nej8PL1UVZt6ZOak1yugukEQ="/>

<a href="/item/1" data-remote="true">view</a>

<%= csrf_meta_tag %>

<%= link_to 'view', item, :remote => true %>

Page 27: jQuery and Rails, Sitting in a Tree

<meta name="csrf-param" content="authenticity_token"/><meta name="csrf-token" content="m3nej8PL1UVZt6ZOak1yugukEQ="/>

<a href="/item/1" data-remote="true">view</a>

<%= csrf_meta_tag %>

<%= link_to 'view', item, :remote => true %>

:confirm and :method also available

Page 28: jQuery and Rails, Sitting in a Tree

rails.js$('form[data-remote]').live('submit', function(e) {  $(this).callRemote();  e.preventDefault();});

$('a[data-remote],input[data-remote]').live('click', function(e) {  $(this).callRemote();  e.preventDefault();});

Page 29: jQuery and Rails, Sitting in a Tree

page.insert_html :bottom, :items, :partial => 'item', :object => @itempage.replace_html :item_count, pluralize(@items.size, 'item')page[:new_item_form].toggle

create.js.rjs

Page 30: jQuery and Rails, Sitting in a Tree

page.insert_html :bottom, :items, :partial => 'item', :object => @itempage.replace_html :item_count, pluralize(@items.size, 'item')page[:new_item_form].toggle

$('#items').append(<%=   render(:partial => 'item', :object => @item).to_json%>);

$('#item_count').html('<%= pluralize(@items.size, "item") %>');

$('#new_item_form').toggle()

create.js.rjs

create.js.erb

Page 31: jQuery and Rails, Sitting in a Tree

JQUERY + RAILS 3

Include jquery.js and rails.js

Include csrf_meta_tags

:remote => true

Render .js.erb templates

✓✓

✓✓

Page 32: jQuery and Rails, Sitting in a Tree

JQUERY + RAILS 3

Include jquery.js and rails.js

Include csrf_meta_tags

:remote => true

Render .js.erb templates

2

X

Page 33: jQuery and Rails, Sitting in a Tree

JQUERY + RAILS 3

Include jquery.js and rails.js

Include csrf_meta_tags

:remote => true

Render .js.erb templates

2

✓✓

X

Page 34: jQuery and Rails, Sitting in a Tree

<meta name="csrf-param" content="authenticity_token"/><meta name="csrf-token" content="m3nej8PL1UVZt6ZOak1yugukEQ="/>

<a href="/item/1" data-remote="true">view</a>

<%= csrf_meta_tag %>

<%= link_to 'view', item, :remote => true %>

Page 35: jQuery and Rails, Sitting in a Tree

<meta name="csrf-param" content="authenticity_token"/><meta name="csrf-token" content="m3nej8PL1UVZt6ZOak1yugukEQ="/>

<a href="/item/1" data-remote="true">view</a>

<%= csrf_meta_tag %>

Rack::Utils.escape_html(request_forgery_protection_token)Rack::Utils.escape_html(form_authenticity_token)

<%= link_to 'view', item, :remote => true %>

Page 36: jQuery and Rails, Sitting in a Tree

<%= link_to 'view', item, 'data-remote' => true %>

<meta name="csrf-param" content="authenticity_token"/><meta name="csrf-token" content="m3nej8PL1UVZt6ZOak1yugukEQ="/>

<a href="/item/1" data-remote="true">view</a>

<%= csrf_meta_tag %>

Rack::Utils.escape_html(request_forgery_protection_token)Rack::Utils.escape_html(form_authenticity_token)

Page 37: jQuery and Rails, Sitting in a Tree

JQUERY + RAILS 3

Include jquery.js and rails.js

Include csrf_meta_tags

:remote => true

Render .js.erb templates

2X

✓✓

Page 38: jQuery and Rails, Sitting in a Tree

JQUERY + RAILS 3

Include jquery.js and rails.js

Reproduce csrf_meta_tags

‘data-remote’ => true

Render .js.erb templates

2X

✓✓

✓✓

Page 39: jQuery and Rails, Sitting in a Tree

Intro to jQuery

jQuery with Rails 3

jQuery with Rails 2

Beyond the Rails (Ajax) Way

AGENDA

✓✓✓

Page 40: jQuery and Rails, Sitting in a Tree

AJAX REQUEST

JAVASCRIPTbrowser app

Page 41: jQuery and Rails, Sitting in a Tree

AJAX REQUEST

JSONbrowser app

Page 42: jQuery and Rails, Sitting in a Tree

<%= link_to 'view', '/item/1', 'data-remote' => true,                                'data-type' => 'json' %>

template

Page 43: jQuery and Rails, Sitting in a Tree

controller  def create    item = Item.create(params[:item])

    render :json => {      :errors     => item.errors,      :item_count => Item.count,      :html       => render_to_string(:partial => 'item', :object => item)    }  end

Page 44: jQuery and Rails, Sitting in a Tree

application.js

$('#new_item_form').live('ajax:success', function(xhr, data) {  if (data.errors.length) {    alert(data.errors);  } else {    $('#items').append(data.html);    $('#item_count').html(data.item_count);    $(this).hide();  }});

Page 45: jQuery and Rails, Sitting in a Tree

rails.js$.ajax({  url: url,  data: data,  dataType: dataType,  type: method.toUpperCase(),  beforeSend: function (xhr) {    el.trigger('ajax:loading', xhr);  },  success: function (data, status, xhr) {    el.trigger('ajax:success', [data, status, xhr]);  },  complete: function (xhr) {    el.trigger('ajax:complete', xhr);  },  error: function (xhr, status, error) {    el.trigger('ajax:failure', [xhr, status, error]);  }});

Page 46: jQuery and Rails, Sitting in a Tree

Intro to jQuery

jQuery with Rails 3

jQuery with Rails 2

Beyond the Rails (Ajax) Way

AGENDA

✓✓✓