Top Banner
UJS Unobtrusive JavaScript
44

Unobtrusive JavaScript

May 17, 2015

Download

Technology

Vitaly Baum

My presentation from the first moscow alt.net user group event
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Unobtrusive JavaScript

UJS Unobtrusive JavaScript

Page 2: Unobtrusive JavaScript

• Software Developer

• Blogger

• Podcaster

• Speaker

[email protected]

www.butaji.ru

Page 3: Unobtrusive JavaScript

Agenda

• What’s the problem

• Patterns

• Rails.js

• Adaptation

– ASP.NET MVC

Page 4: Unobtrusive JavaScript
Page 5: Unobtrusive JavaScript

.ajax

Page 6: Unobtrusive JavaScript

What’s the problem

• Server-side JavaScript generation

– GWT

– Script#

Page 7: Unobtrusive JavaScript

What’s the problem

• A lot of JavaScript frameworks

– jQuery

– Prototype

Page 8: Unobtrusive JavaScript

Patterns

just step on a rake

Page 9: Unobtrusive JavaScript

Template Patterns

• Double templating

• Just Ajax

• Update panel

• Loaded JavaScript

Page 10: Unobtrusive JavaScript

Double templating

• Server side

– Page layout

– Custom DSLs

[Haml]

• Client side

– Async

Page 11: Unobtrusive JavaScript

Double templating

<!-- Server side -->

<h2><%= product.Name %></h2>

<p><%= product.Description %></p>

Page 12: Unobtrusive JavaScript

Double templating

<script type="text/javascript">

function addProduct(prod) {

var pDiv = "<h2>" + prod.Title + "</h2>" + "<p>" + prod.Descr + "</p>";

$("#items").append(pDiv);

}

</script>

Page 13: Unobtrusive JavaScript

Double templating

• Less client cpu load

• Less traffic

• Different places with markup

Page 14: Unobtrusive JavaScript

Just Ajax

JavaScript JSON/XML JavaScript HTML

Server

Page 15: Unobtrusive JavaScript

Just Ajax

JavaScript JSON/XML JavaScript HTML

Server

<div id="items"></div> $.ajax(…)

Page 16: Unobtrusive JavaScript

Just Ajax

JavaScript JSON/XML JavaScript HTML

Server

{[ { "item": { "title": "product 1"} }, { "item": { "title": "product 2" } } ]}

Page 17: Unobtrusive JavaScript

Just Ajax

JavaScript JSON/XML JavaScript HTML

Server

$("#items").appentTo(pDiv);

Page 18: Unobtrusive JavaScript

Just Ajax

JavaScript JSON/XML JavaScript HTML

Server

<div id="items"> <h2>product 1</h2> <h2>product 2</h2> </div>

Page 19: Unobtrusive JavaScript

Just Ajax

• More client cpu load

• Less time on page load

• Less traffic

Page 20: Unobtrusive JavaScript

Update panel

• Full page post-back

• Partial page refreshing

Page 21: Unobtrusive JavaScript

Update panel

Client Server

Magic JS

John

Total

0

1000$

OK

Page 22: Unobtrusive JavaScript

Update panel

Client Server

John

1000$

Total

0

OK

Code

Magic JS

Page 23: Unobtrusive JavaScript

Update panel

Client Server

John

Total

0

Code

Magic JS

OK

1000$

Page 24: Unobtrusive JavaScript

Update panel

• More traffic/complexity

• Automatic management

• Constraints

• Less work

Page 25: Unobtrusive JavaScript

Loaded JavaScript

• Partial post-back

• Partial refreshing

Page 26: Unobtrusive JavaScript

Loaded JavaScript

Client Server

John

1000$

Total

0

OK

Code

Hand-written JS

Page 27: Unobtrusive JavaScript

Loaded JavaScript

• Fine tuning

• Not too much work

• One template

Page 28: Unobtrusive JavaScript

Rails.js my pretty love

Page 29: Unobtrusive JavaScript

Unobtrusive JavaScript

in Rails 2.3 remote_form_for(@item)

Page 30: Unobtrusive JavaScript

Unobtrusive JavaScript

in Rails 2.3 remote_form_for(@item)

<form action="/posts" class="new_post“ id="new_post" method="post" onsubmit="new Ajax.Request( '/posts', {asynchronous:true, evalScripts:true, parameters: Form.serialize(this)});

return false;">

Page 31: Unobtrusive JavaScript

Unobtrusive JavaScript

in Rails 2.3 link_to 'Destroy', @item, :confirm => 'Are you sure?',:method => :delete

Page 32: Unobtrusive JavaScript

Unobtrusive JavaScript

in Rails 2.3 link_to 'Destroy', @item, :confirm => 'Are you sure?',:method => :delete

<a href="/items/1" onclick="if (confirm('Are you sure?')) { 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', 'LM2fEF6HuRWdYUZdEumWlemhI6iDPH97pqWhO4jEpiU='); f.appendChild(s);f.submit(); };return false;">Destroy</a>

Page 33: Unobtrusive JavaScript

data-* works even in IE6

Page 34: Unobtrusive JavaScript

HTML 5 custom data attributes

• data-remote

• data-method

• data-confirm

• data-disable-with

Page 35: Unobtrusive JavaScript

Unobtrusive JavaScript

in Rails 3 form_for(@item, :remote => true)

Page 36: Unobtrusive JavaScript

Unobtrusive JavaScript

in Rails 3 form_for(@item, :remote => true)

<form action="/items" class="new_item" data-remote="true" id="Form1" method="post">

Page 37: Unobtrusive JavaScript

Unobtrusive JavaScript

in Rails 3 link_to 'Destroy', @item, :confirm => 'Are you sure?',:method => :delete

Page 38: Unobtrusive JavaScript

Unobtrusive JavaScript

in Rails 3 link_to 'Destroy', @item, :confirm => 'Are you sure?',:method => :delete

<a href="/items/1"

data-confirm="Are you sure?"

data-method="delete“

rel="nofollow">Destroy</a>

Page 39: Unobtrusive JavaScript

Unobtrusive JavaScript

HTML Custom Data Attributes

JavaScript Driver

JavaScript Framework

Page 41: Unobtrusive JavaScript

What if I want

UJS into my framework

Page 42: Unobtrusive JavaScript

ASP.NET Adaptation

Page 43: Unobtrusive JavaScript

Summary

• What’s the problem

• Patterns

• Rails.js

• Adaptation

– ASP.NET MVC