SlideShare a Scribd company logo
JS for Rails developers
        Timur Vafin
Стандарт Google
Стандарт Google


• lowerCamelCase
Стандарт Google


• lowerCamelCase
• 2 пробела
Стандарт Google


• lowerCamelCase
• 2 пробела
• http://google-styleguide.googlecode.com
JSON vs. JS
# view
<%= form_for Comment.new, :remote => true do |form| %>




# controller
def create
  @comment = @post.comments.create(params[:commnet])
end




# create.js.erb
$('comments.new').before('<%=j render @comment %>');
$('comments.new').val('');
<3
<3
<3
<3
<3
Unobtrusive JavaScript
<a href="#" onclick="alert('Hello world!'); return false;">
  Click Here
</a>
<a href="#" onclick="alert('Hello world!'); return false;">
  Click Here
</a>




<script type="text/javascript"charset="UTF-8">
  $(function () {
    $('#alert').click(function () {
       alert(this.data('message'));
       return false;
    })
  });
</script>

<a href="#" id="alert" data-message="Hello from UJS">Click Here</a>
WTF?
JS for Rails developers
JS for Rails developers
<a href="#" id="alert" data-message="Hello from UJS">
  Click Here
</a>
<a href="#" id="alert" data-message="Hello from UJS">
  Click Here
</a>




<a href="/posts/8" data-confirm="Are you sure?" data-method="delete">
  Destroy
</a>
<a href="#" id="alert" data-message="Hello from UJS">
  Click Here
</a>




<a href="/posts/8" data-confirm="Are you sure?" data-method="delete">
  Destroy
</a>




<form action="/products" data-remote="true" method="get">
<% content_for :head do %>
  <%= javascript_include_tag 'my_super_plugin' %>

 <script type="text/javascript" charset="UTF-8">
   $(document).ready(function() {
      $('form[data-autosubmit=true]').autoSubmit();
   })
 </script>

<% end %>

<%= yield :head %>
JS for Rails developers
<%= div_for(@person, :class => "foo") %>

<div id="person_123" class="person foo">
<%= div_for(@person, :class => "foo") %>

<div id="person_123" class="person foo">




<%= content_tag_for(:li, @person) do %>

<li id="person_123" class="person">
<%= div_for(@person, :class => "foo") %>

<div id="person_123" class="person foo">




<%= content_tag_for(:li, @person) do %>

<li id="person_123" class="person">




dom_id(Post.find(45))          #   =>   "post_45"
dom_id(Post.new)               #   =>   "new_post"
dom_id(Post.find(45), :edit)   #   =>   "edit_post_45"
dom_class(post)                #   =>   "post"
dom_class(Person)              #   =>   "person"
Модульность
var js = {
  namespace1: {
    function1: function() {
      alert('Call me!');
    }
  }
}
JQuery UI Plugin
JQuery UI Plugin

• опции по умолчанию
JQuery UI Plugin

• опции по умолчанию
• приватные и публичные методы
JQuery UI Plugin

• опции по умолчанию
• приватные и публичные методы
• конструктор и деструктор
JS for Rails developers
JS for Rails developers
$(document).ready ->
  $('form[data-autosubmit=true]').autoSubmit()
$(document).ready ->
  $('form[data-autosubmit=true]').autoSubmit()




js =
  namespace:
    test: ->
      alert 'test'

js.namespace.test();
$(document).ready ->
  $('form[data-autosubmit=true]').autoSubmit()




js =
  namespace:
    test: ->
      alert 'test'

js.namespace.test();




$('comments.new').before '<%=j render @comment %>'
$('comments.new').val ''
JS for Rails developers
//= require jquery
//= require my_super_plugin

# my_super_plugin
//= require validation
//= require defaults
PJAX

More Related Content

PDF
Javascript and jQuery for Mobile
PDF
jQuery - Javascript para quem não sabe Javascript
PDF
WordPress Customizer
PDF
Modernizerを用いたHTML5Webサイトの構築
PDF
UI実装におけるコーディングあれこれ
PPT
Palestra sobre MongoDB com PHP no PHP'n'Rio
PDF
Drupal 8. Movement towards. Susikov Sergey
PDF
Crea un tema compatibile con le ultime novità WordPress
Javascript and jQuery for Mobile
jQuery - Javascript para quem não sabe Javascript
WordPress Customizer
Modernizerを用いたHTML5Webサイトの構築
UI実装におけるコーディングあれこれ
Palestra sobre MongoDB com PHP no PHP'n'Rio
Drupal 8. Movement towards. Susikov Sergey
Crea un tema compatibile con le ultime novità WordPress

What's hot (20)

KEY
jQuery入門
TXT
Formulario
TXT
PDF
Jquery Framework
PPT
jQuery - Write less, do more!
PDF
Introducción a Bolt
PPT
Tools20121015
PPTX
Základy jQuery
DOCX
Un juego creado en php
PPT
Wek14 mysql 2
KEY
SmartCSS
DOCX
The Tab widget
DOCX
The Small Widget
PDF
Testing in JavaScript
PDF
观剧卡免费赠读者
RTF
Como colokar xat clikado
PDF
Domótica. Test de conocimientos
PDF
How to Win the Heart of CSS Boys
PDF
Jquery2
PDF
Peek inside the fantastical Ukrainian Village home and studio of artists Jare...
jQuery入門
Formulario
Jquery Framework
jQuery - Write less, do more!
Introducción a Bolt
Tools20121015
Základy jQuery
Un juego creado en php
Wek14 mysql 2
SmartCSS
The Tab widget
The Small Widget
Testing in JavaScript
观剧卡免费赠读者
Como colokar xat clikado
Domótica. Test de conocimientos
How to Win the Heart of CSS Boys
Jquery2
Peek inside the fantastical Ukrainian Village home and studio of artists Jare...
Ad

Viewers also liked (9)

PPT
Japanese zeke presentation
PPTX
Pomodoro
PPT
Japanese zeke presentation
PPTX
Hannah Reilly Foundation Portfolio Evaluation PowerPoint
PPT
우주 배경 복사와 우주 거대 구조
PPTX
PPT
도시 열섬 현상
PPTX
ICON 2010 3D 게임에서 색차 방식의 3D 입체 영상 구현하기
PPT
3D Engine (ICON 2007)
Japanese zeke presentation
Pomodoro
Japanese zeke presentation
Hannah Reilly Foundation Portfolio Evaluation PowerPoint
우주 배경 복사와 우주 거대 구조
도시 열섬 현상
ICON 2010 3D 게임에서 색차 방식의 3D 입체 영상 구현하기
3D Engine (ICON 2007)
Ad

JS for Rails developers

Editor's Notes