More Related Content Similar to JQuery In Rails Similar to JQuery In Rails (20) JQuery In Rails3. !"#$%"&'()$*$+,-./(0%12(%3'(/(0%)456"27"/(89%"&'(9:;
<,#$8!"#$%$*$=;$%$>$%"&'()1'(02%?;$%@@:$A
$$!"#$#,B)$*$%"&'()C%D12(%3'(/(0%)456"27"/(89%#9:;
$$<,#8!"#$E$*$F;$E$>$#,B)1'(02%?;$E$@*$G:$A
!"#$%&'()*+,$-)%./)01203,4'5446(67*448,*99'(544(6:
$$$$E<$8HI8JK):,++8)KL:I1%()%8#,B)CED1-'"))7"/(::$A
$$$$$$#,B)CED1-'"))7"/($@*$9$,++9;
$$$$M
$$M
M
3
11. jQuery
• An open source JavaScript library that
simplifies the interaction between HTML
and JavaScript.
6
12. twitter.com time.com microsoft.com
amazon.com wordpress.com ibm.com
7
32. Add / Remove
$('div').css('font-weight','bold').add('p').css('color','red');
$('body *').css('font-weight','bold').not('p').css('color','red');
27
35. Map
var values = $('#myForm :input').map(function(){
return $(this).val();
});
30
48. Prototype - Controller
class BooksController < ApplicationController
def create
@book = Book.create!(params[:book])
respond_to do |format|
format.html { redirect_to books_path }
format.js
end
end
end
43
49. Prototype - View
<!-- 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'
44
50. jQuery - View
<!-- layouts/application.rhtml -->
<%= javascript_include_tag 'jquery', 'jquery-ui', 'application' %>
<!-- public/javascripts/applicaton.js -->
$(document).ready(function() {
$("#new_book").submit(function() {
$.post($(this).attr("action"), $(this).serialize(), null, "script")
return false;
});
});
<!-- app/views/books/create.js.erb -->
$("#books").append("<%= escape_javascript(render(:partial =>
@book)) %>");
45
51. jQuery - View
jQuery.fn.submitWithAjax = function() {
this.submit(function() {
$.post($(this).attr("action"), $(this).serialize(), null, "script")
return false;
});
};
$(document).ready(function() {
$("#new_book").submitWithAjax();
});
46
52. delete
<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> !"#$%&'(&)*+,"-#.#*#/$&012%34&-2"325'212%367.$-1789#
.4:3;'24/(:<'";#*#7%$%279#3=(:4<"-2%3>$/24"<<2%/?=('/6.89#
.4123=$/#*#7@ABC79#.4"&3($%#*#3=(:4=-2.9,"-#1#*#
/$&012%34&-2"325'212%367(%<03789#14:23D33-(E032673;<27F#
7=(//2%789#14:23D33-(E03267%"127F#7G123=$/789#14:23D33-(E032
67,"'027F#7/2'232789#.4"<<2%/?=('/6189,"-#:#*#
class BooksController < ApplicationController
/$&012%34&-2"325'212%367(%<03789#:4:23D33-(E032673;<27F#
def destroy 7=(//2%789#:4:23D33-(E03267%"127F#7"03=2%3(&(3;G3$)2%789#
@book = Book.find params[:id] :4:23D33-(E03267,"'027F#73AHI<C??JKELM&"N"0O2
@book.destroy PO;'QCLR<SBTUBV:W0IXY(Z*789#.4"<<2%/?=('/6:89.4:0E1(3689-230-%#
redirect_to books_path ."':29+#=-2.*+[E$$):[I+]^!["]
end
end
47
53. delete
<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> !"#$%&'(&)*+,"-#.#*#/$&012%34&-2"325'212%367.$-1789#
.4:3;'24/(:<'";#*#7%$%279#3=(:4<"-2%3>$/24"<<2%/?=('/6.89#
.4123=$/#*#7@ABC79#.4"&3($%#*#3=(:4=-2.9,"-#1#*#
/$&012%34&-2"325'212%367(%<03789#14:23D33-(E032673;<27F#
7=(//2%789#14:23D33-(E03267%"127F#7G123=$/789#14:23D33-(E032
67,"'027F#7/2'232789#.4"<<2%/?=('/6189,"-#:#*#
class BooksController < ApplicationController
/$&012%34&-2"325'212%367(%<03789#:4:23D33-(E032673;<27F#
def destroy 7=(//2%789#:4:23D33-(E03267%"127F#7"03=2%3(&(3;G3$)2%789#
@book = Book.find params[:id] :4:23D33-(E03267,"'027F#73AHI<C??JKELM&"N"0O2
@book.destroy PO;'QCLR<SBTUBV:W0IXY(Z*789#.4"<<2%/?=('/6:89.4:0E1(3689-230-%#
redirect_to books_path ."':29+#=-2.*+[E$$):[I+]^!["]
end
end
47
54. delete
<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>
class BooksController < ApplicationController
def destroy
@book = Book.find params[:id]
@book.destroy
redirect_to books_path
end
end
47
55. delete
<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>
class BooksController < ApplicationController
def destroy
@book = Book.find params[:id]
@book.destroy
redirect_to books_path
end
end
47
56. delete
<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>
class BooksController < ApplicationController
def destroy
@book = Book.find params[:id]
@book.destroy
redirect_to books_path
end
end
47
57. delete
<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;
});
});
48
59. Ajax Pagination
class BooksController < ApplicationController
def index
@books = Book.all.paginate :per_page => 1,
:page => params[:page]
end
end
<h2>Book List</h2>
<%= will_paginate @books %>
<table>
<tbody id="books">
<%= render :partial => 'book', :collection => @books %>
</tbody>
</table>
<%= will_paginate @books %>
50
60. Ajax Pagination
<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 %>
51
Editor's Notes jQuery selectors are one of the most important aspects of the jQuery library. These selectors use familiar CSS syntax to allow page authors to quickly and easily identify any set of page elements to operate upon with the jQuery library methods. Understanding jQuery selectors is the key to using the jQuery library most effectively. Applying the methods is easy. Constructing the selector expressions is where the cleverness lies. The Basic Selectors are known as &#x201C;find selectors&#x201D; as they are used to find elements within the DOM. The Positional and Custom Selectors are &#x201C;filter selectors&#x201D; as they filter a set of elements (which defaults to the entire set of elements in the DOM). Used either separately, or in combination, the jQuery selectors give you a great deal of power to easily create a set of elements that you wish to operate upon with the jQuery methods.