53
jQuery in Rails Louie Zhao Co - Founder Caibangzi.com & IN-SRC Studio 1

Jquery In Rails

Embed Size (px)

DESCRIPTION

By Zhao Lu

Citation preview

Page 1: Jquery In Rails

jQuery in RailsLouie Zhao

Co - Founder

Caibangzi.com & IN-SRC Studio

1

Page 2: Jquery In Rails

Problem

• JavaScript programming can be hard

• Completely dynamic language

• JavaScript, by itself, isn’t useful

2

Page 3: Jquery In Rails

3

var tables = document.getElementsByTagName('table');for (var t = 0; t < tables.length; t++) { var rows = tables[t].getElementsByTagName('tr'); for(var i = 1; i < rows.length; i += 2) { if (!/(^¦s)odd(s¦$)/.test(rows[i].className)) { rows[i].className += ' odd'; } }}

jQuery('table tr:nth-child(odd)').addClass('odd');

Page 4: Jquery In Rails

JavaScript Library

• Drastically simplify DOM, Ajax, Animation

4

Page 5: Jquery In Rails

5

Page 6: Jquery In Rails

jQuery

• An open source JavaScript library that simplifies the interaction between HTML and JavaScript.

6

Page 7: Jquery In Rails

7

time.comtwitter.com

wordpress.comamazon.com ibm.com

microsoft.com

Page 8: Jquery In Rails

8

Page 9: Jquery In Rails

jQuery

• Cross Browser

• Small

• Lean API, fun coding

9

Page 10: Jquery In Rails

jQuery Overview

• Selector & Chaining

• DOM manipulation

• Events

• Effects

• Ajax

10

Page 11: Jquery In Rails

Selector & Chaining

11

$(selector).method1().method2().method3();

$('#goAway').hide().addClass('incognito');

Page 12: Jquery In Rails

Selector

12

• Basic CSS selectors

• Positional selectors

• Custom jQuery selectors

Page 13: Jquery In Rails

Basic CSS Selector

13

$('li>p')

Page 14: Jquery In Rails

Basic CSS Selector

14

$('div.someClass')

Page 15: Jquery In Rails

Basic CSS Selector

15

$('#testButton')

Page 16: Jquery In Rails

Basic CSS Selector

16

$('img[alt]')

Page 17: Jquery In Rails

Basic CSS Selector

17

$('a[href$=.pdf]')

Page 18: Jquery In Rails

Basic CSS Selector

18

$('button[id*=test]')

Page 19: Jquery In Rails

Positional Selectors

19

$('p:first')

Page 20: Jquery In Rails

Positional Selectors

20

$('img[src$=.png]:first')

Page 21: Jquery In Rails

Positional Selectors

21

$('button.small:last')

Page 22: Jquery In Rails

Positional Selectors

22

$('li:first-child')

Page 23: Jquery In Rails

Positional Selectors

23

$('a:only-child')

Page 24: Jquery In Rails

Positional Selectors

24

$('li:nth-child(2)')$('li:nth-child(odd)')$('li:nth-child(5n)')$('li:nth-child(5n+1)')

Page 25: Jquery In Rails

Positional Selectors

25

$('.someClass:eq(1)')$('.someClass:gt(1)')$('.someClass:lt(4)')

Page 26: Jquery In Rails

Custom Selectors

26

$(':button:hidden')$('input[name=myRadioGroup]:radio:checked')$(':text:disabled')$('#xyz p :header')$('option:not(:selected)')$('#myForm button:not(.someClass)')$('select[name=choices] :selected')$('p:contains(coffee)')

Page 27: Jquery In Rails

Add / Remove

27

$('div').css('font-weight','bold').add('p').css('color','red');

$('body *').css('font-weight','bold').not('p').css('color','red');

Page 28: Jquery In Rails

Find / Slicing

28

$('div').css('background-color','blue').find('img').css('border','1px solid aqua');

$('body *').slice(2,3).hide();

Page 29: Jquery In Rails

Matching by Relationship

29

.parents("tr:first")

Page 30: Jquery In Rails

Map

30

var values = $('#myForm :input').map(function(){ return $(this).val();});

Page 31: Jquery In Rails

Controlling Chaining

31

$('div').add('p').css('color','red').end().hide();

$('div').css('background-color','yellow').children('img').css('border','4px ridge maroon').andSelf().css('margin','4em');

Page 32: Jquery In Rails

DOM Manipulation

• Changing contents

32

.html("<p>This is a paragraph.</p>")

.text("<p>This is a paragraph.</p>")

Page 33: Jquery In Rails

DOM Manipulation

• Inserting inside

• append(content)

• appendTo(selector)

• prepend(content)

• prependTo(selector)

33

Page 34: Jquery In Rails

DOM Manipulation

• Inserting outside

• after(content)

• insertAfter(selector)

• before(content)

• insertBefore(selector)

34

Page 35: Jquery In Rails

DOM Manipulation

• Inserting around

• Replacing

• Removing

• Copying

35

Page 36: Jquery In Rails

Events

• Page Load

• Event Handling

• Live Events

• Event Helpers

36

Page 37: Jquery In Rails

Events

• Run after DOM, but before images

37

$(document).ready(function() {});

Page 38: Jquery In Rails

Effects

38

Page 39: Jquery In Rails

Ajax

• Cross browser

39

$.ajax();

$.load();

$.get();

$.post();

Page 40: Jquery In Rails

jQuery UI

• Interactions

• Widgets

40

Page 41: Jquery In Rails

Interactions

• Draggable

• Droppable

• Resizable

• Selectable

• Sortable

41

Page 42: Jquery In Rails

Widgets

• Accordion

• Date picker

• Dialog

• Progress Bar

• Slider

• Tabs

42

Page 43: Jquery In Rails

Prototype - Controller

43

class BooksController < ApplicationController def create @book = Book.create!(params[:book]) respond_to do |format| format.html { redirect_to books_path } format.js end endend

Page 44: Jquery In Rails

Prototype - View

44

<!-- layouts/application.rhtml --><%= javascript_include_tag :defaults %>

<!-- layouts/show.rhtml --><% form_remote_for :book, :url => books_path %> ....<% end %>

<!-- app/views/books/create.js.rjs -->page.insert_html :bottom, :books, :partial => 'book'

Page 45: Jquery In Rails

jQuery - View

45

<!-- app/views/books/create.js.erb -->$("#books").append("<%= escape_javascript(render(:partial => @book)) %>");

<!-- public/javascripts/applicaton.js -->$(document).ready(function() { $("#new_book").submit(function() { $.post($(this).attr("action"), $(this).serialize(), null, "script") return false; }); });

<!-- layouts/application.rhtml --><%= javascript_include_tag 'jquery', 'jquery-ui', 'application' %>

Page 46: Jquery In Rails

jQuery - View

46

jQuery.fn.submitWithAjax = function() { this.submit(function() { $.post($(this).attr("action"), $(this).serialize(), null, "script") return false; }); };

$(document).ready(function() { $("#new_book").submitWithAjax();});

Page 47: Jquery In Rails

delete

47

class BooksController < ApplicationController def destroy @book = Book.find params[:id] @book.destroy redirect_to books_path endend

<tr id="book_<%= book.id %>"> <td><%= h book.title %></td> <td><%= h book.author %></td> <td><%= book.price %></td> <td><%= book.published_on.to_s %></td> <td><%= link_to 'X', book_path(book), :method => :delete %></td></tr> <a onclick="var f = document.createElement('form');

f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);var s = document.createElement('input'); s.setAttribute('type', 'hidden'); s.setAttribute('name', 'authenticity_token'); s.setAttribute('value', 'tOF2pTCCgIb1VcazauJe+JylUT1jpWSK8SLsBu2YDiw='); f.appendChild(s);f.submit();return false;" href="/books/23">X</a>

Page 48: Jquery In Rails

delete

48

<td> <%= link_to 'X', book_path(book), :class => "delete" %></td>

$(document).ready(function() { $("a.delete").click(function(){ $.ajax({ type: "DELETE", url: this.href }); $(this).parents("tr:first").remove(); return false; });});

Page 49: Jquery In Rails

Date Picker

49

$("#book_published_on").datepicker();

Page 50: Jquery In Rails

Ajax Pagination

50

<h2>Book List</h2><%= will_paginate @books %><table> <tbody id="books"> <%= render :partial => 'book', :collection => @books %> </tbody> </table><%= will_paginate @books %>

class BooksController < ApplicationController def index @books = Book.all.paginate :per_page => 1, :page => params[:page] endend

Page 51: Jquery In Rails

Ajax Pagination

51

<h2>Book List</h2><div id="paginated_books"><%= render :partial => 'books' %></div>

<%= will_paginate @books %><table> <tbody id="books"> <%= render :partial => 'book', :collection => @books %> </tbody> </table><%= will_paginate @books %>

Page 52: Jquery In Rails

Ajax Pagination

52

$(document).ready(function() { $(".pagination a").live("click", function() { $(".pagination").html("loading...."); $.get(this.href, null, null, "script"); return false; });});

$("#paginated_books").html("<%= escape_javascript(render('books')) %>")

index.js.erb

application.js

.click(function() ....live(“click”, function() ...

Page 53: Jquery In Rails

Thanks!

53