jQuery Mobile for dummies
(featuring AC/DC's Let there be rock)
Daniel de la Mata
[email protected] @danimataonrails danimataonrails.blogspot.com camerawc.guatdefak.com yumit.com/danimataIn the beginning, back in nineteen fifty five
In the beginning, back in nineteen fifty five
www.facturagem.com
Rails 3
jQuery 1.5.1 / jQuery-ui 1.8.12
Restful API
Man didn't know 'bout a rock 'n' roll show
Man didn't know 'bout a rock 'n' roll show
'N all that jive
'N all that jive
mobile.facturagem.com
Tecnología a elegir
Interfaz móvil
API demo
***
1 persona / 2 semanas
***
The white man had the schmaltz, The black man
The white man had the schmaltz, The black man
had the blues
had the blues
alternativas
Spine.js / Backbone.js
Sencha Touch
jQuery Mobile
JQTouch
***
Titanium
PhoneGap
No one knew what they were gonna do but
No one knew what they were gonna do but
Tschaikovsky had the news
Tschaikovsky had the news
entorno
Windows IDE = [consola, notepad++, gimp]
conocimiento previo, puesta en marcha rápida
jQuery / jQuery UI
soporte, documentación, apoyo
jquerymobile.com
y...
He said...
He said...
jQuery and jQuery UI
Compatible (A, B, C)
HTML5
Ligero / Modular
Progressive Enhancement
Ajax Navigation / One page apps
(silence)
(silence)
Ejemplos
http://danimata.com/codemotion/examples.html
Todo y más en
http://jquerymobile.com/
Let there be sound...
Let there be sound...
<!DOCTYPE html>
<html> <head>
<title>Este es mi título (que luego igual no se ve)</title>
<meta name="viewport" content="width=device-width,
initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.css" /> <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.js"></script> </head> <body> ... Y este es mi contenido ... </body> </html>
and there was sound
and there was sound
Let there be light...
Let there be light...
<div
data-role
="
page
">
<div
data-role
="
header
">
<h1>
Cabecera de la página
</h1>
</div>
<!-- /header -->
<div
data-role
="
content
">
<p>
Contenido de la página
</p>
</div>
<!-- /content -->
<div
data-role
="
footer
">
<h4>
Pie de página
</h4>
</div>
<!-- /footer -->
...and there was light
...and there was light
Let there be drums...
Let there be drums...
<div data-role="page" id="one"
data-title="Uno">
<div data-role="header" data-position="fixed">
<h1>Uno</h1>
</div><!-- /header →
<div data-role="content">
<p><a href="#two">dos</a></p> <p><a
href="#three">tres</a></p> </div><!-- /content →
<div data-role="footer" data-position="fixed">
<h4>Pie</h4>
</div><!-- /footer →
</div><!-- /page -->
<div data-role="page" id="two"> ...
<p><a href="#one">volver</a></p> ...
<div data-role="page" id="three"> ...
<p><a href="#jander" data-rel="back"> volver</a></p> ...
...and there was drums
...and there was drums
Let there be guitar...
Let there be guitar...
<div data-role="page" id="one" data-title="Uno">
<div data-role="header" data-position="fixed">
<h1>Uno</h1>
</div><!-- /header →
<div data-role="content">
<p><a href="#two">dos</a></p> <p><a href="#three"
onclick="$.mobile.changePage('#three');
return false;">tres</a></p>
<p><a href="#pop" data-rel="dialog">pop up</a></p>
</div><!-- /content →
<div data-role="footer" data-position="fixed">
<h4>Pie</h4>
</div><!-- /footer →
</div><!-- /page -->
<div data-role="page" id="two" ...>
...
<p><a href="#jander" data-rel="back">
volver</a></p>
...
<div data-role="page" id="three" ...>
...
<p><a href="#one"
data-direction="reverse">volver</a></p>
...
<div data-role="page" id="pop" ...>
...
<div data-role="content">
<p><a href="#one">volver</a></p>
<p><a href="#jander" data-rel="back">volver
</a></p>
<p><a href="#one" data-direction="reverse">
volver</a></p>
<p><a href="#klander" onclick=" $('.ui-dialog').dialog('close'); return false;">
cerrar</a></p>
...
...and there was guitar
...and there was guitar
Let there be Rock!
Let there be Rock!
(guitar solo)
(guitar solo)
Transiciones y modales
<a
href
="popup.html"
data-transition
="pop"
>Pop it</a>
<a
href
="alert.html"
data-rel
="dialog"
>Alert!</a>
$mobile.defaultPageTransition = 'pop'; $mobile.defaultPageTransition = 'slide';
['fade', 'pop', 'flip', 'turn', 'flow', 'slide', 'slideUp', slideDown']
$.mobile.changePage('#id');
http://jquerymobile.com/demos/1.1.0-rc.1/docs/pages/page-transitions.html
Precarga
<a
href
="prefetchThisPage.html"
data-prefetch
>Enlace</a>
And it came to pass that rock 'n' roll was born
And it came to pass that rock 'n' roll was born
Botones I
Tags button, input[type=button] e input[type=submit] reciben estilo automáticamente
$('[type='submit']').button();
//just in case
Los enlaces pueden coger el mismo estilo
<a
href
="index.html"
data-role
="button"
>
Ir
</a>
Algunas opciones
data-corners
="true|false"
data-inline
="true|false"
data-shadow
="true|false"
All across the land every rockin' band
All across the land every rockin' band
Botones II
Iconos predefinidos y customizables
<a
href
="index.html"
data-icon
="delete"
>
Ir
</a>
Predefinidos
arrow-r, arrow-l, arrow-u, arrow-d, delete, plus, minus,
check, gear, refresh, forward, back, grid, star, alert,
home, search
Algunas opciones
data-iconpos
="left|right|bottom|top|notext"
Was blowin' up a storm
Was blowin' up a storm
Botones III
Grupos
<div
data-role
="controlgroup"
>
<a
href
="#index"
data-role
="button">
Yes
</a>
<a
href
="#index"
data-role
="button">
No
</a>
<a
href
="#index"
data-role
="button">
Maybe
</a>
</div>
Horizontal
The guitar man got famous,the business man got rich
The guitar man got famous,the business man got rich
And in every bar there was a superstar
And in every bar there was a superstar
Header Toolbar
<div
data-role
="header"
data-position
="fixed">
<a
href
="#none"
data-icon
="back">
Volver
</a>
<h1
>Hader bar</
h1>
<a
href
="#none"
data-icon
="forward">Seguir</a>
</div>
<!-- /header -->
Algunas opciones
With a seven year itch
With a seven year itch
Footer Toolbar
<div
data-role
="footer"
data-position
="fixed"
class="ui-bar"
>
<a
href
="index.html"
data-role
="button"
data-icon="arrow-u">
Subir
</a>
<a
href
="index.html"
data-role
="button"
data-icon
="arrow-d">
Bajar
</a>
<a
href
="index.html"
data-role
="button"
data-icon
="arrow-l">
Izquierda
</a>
<a
href
="index.html"
data-role
="button"
data-icon
="arrow-r">
Derecha
</a>
</div><!-- /footer -->
Algunas opciones
There was fifteen million fingers
There was fifteen million fingers
Navbar
<div
data-role
="navbar"
>
<ul>
<li><a
href
="#none"
data-role
="button"
data-icon
="plus"
class
="ui-btn-active"
>
añadir
</a></li>
<li><a
href
="#none"
data-role
="button"
data-icon
="minus">
eliminar
</a></li>
<li><a
href
="#none"
data-role
="button"
data-icon
="check">
aceptar
</a></li>
</ul>
</div><!-- /navbar -->
Learnin' how to play
Learnin' how to play
And you could hear the fingers pickin'
And you could hear the fingers pickin'
Listas
<ul
data-role
="listview"
>
<li><a
href
="#one">
Uno
</a></li>
<li><a
href
="#two">
Dos
</a></li>
<li><a
href
="#three">
Tres
</a></li>
</ul>
Numeradas
<ol
data-role
="listview"
>...</ol>
Separadores
<li
data-role
="list-divider"
>...</li>
Filtro
And this is what they had to say
And this is what they had to say
Let there be light
Let there be light
Formularios I
<form action="#" method="post">
<div data-role="fieldcontain" class="ui-hide-label"> <label for="login">login</label>
<input type="text" name="login" id="login" value=""
placeholder="login"/> </div>
<div data-role="fieldcontain" class="ui-hide-label"> <label for="password">password</label>
<input type="password" name="password" id="password" value=""
placeholder="password"/> </div>
<div data-role="fieldcontain" class="button"> <button type="submit">acceder</button>
</div> </form>
sound
sound
Formularios II
<input type="text" name="a" id="a" value="" placeholder="text input"/> <textarea name="b" id="b" value="" placeholder="text area"></textarea> <input type="search" name="c" id="c" value="" />
<input type="email" name="e" id="e" value="" placeholder="email"/> <input type="tel" name="t" id="t" value="" placeholder="teléfono"/> <select name="z" id="z">
<option value="s">pequeño</option> <option value="m">mediano</option> <option value="l">grande</option> </select>
drums
drums
Formularios III
<select name="b" id="b" data-role="slider"> <option value="off">Nole</option>
<option value="on">Síle</option> </select>
<input type="range" name="a" id="a" value="15" min="0" max="100" step="5" data-highlight="true"/>
<input type="checkbox" name="d" id="d"/> <div data-role="controlgroup">
<input type="radio" name="r1" id="c1" value="1" checked="checked" /> <label for="c1">Piedra</label>
<input type="radio" name="r1" id="c2" value="2" /> <label for="c2">Papel</label>
<input type="radio" name="r1" id="c3" value="3" /> <label for="c3">Tijera</label>
and guitar
and guitar
Let there be Rock!
Let there be Rock!
(guitar solo)
(guitar solo)
Markup
<form action="#" method="post">
<input type="hidden" name="format" id="format" value="json"/>
<input type="hidden" name="api_version" id="api_version" value="1"/> <div data-role="fieldcontain" class="ui-hide-label">
<label for="login">login</label>
<input type="text" name="login" id="login" value="" placeholder="login"/>
</div>
<div data-role="fieldcontain" class="ui-hide-label"> <label for="password">password</label>
<input type="password" name="password" id="password" value="" placeholder="password"/>
</div>
<div data-role="fieldcontain" class="button"> <button type="submit">acceder</button>
</div> </form>
(Angus' on the floor)
(Angus' on the floor)
Js
$("#login-form").submit(function(event){
event.preventDefault();
$.ajax('https://facturagem.com/api/session',
{
type: '
POST
',
dataType: '
json
',
cache: false,
data:
$(this).serialize()
,
success: function(data) {
},
error: function(){
}
}
);
(Angus' flying)
(Angus' flying)
One night in a club called the Shakin' Hand
One night in a club called the Shakin' Hand
CORS
cross origin resource sharing
http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing
●
El navegador envía una petición OPTIONS (pide permiso)
●El servidor la recibe, toma una decisión y la envía
(permisos, recursos, verbos, expiry...)
●
Si el servidor concede permiso, el navegador vuelve a
enviar a petición
Respuesta
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS,
DELETE, PUT'
headers['Access-Control-Allow-Headers'] = '*'
headers['Access-Control-Max-Age'] = '1728000'
There was a 42 decibel rockin' band
There was a 42 decibel rockin' band
json
"user": {
"name": "Daniel de la Mata Lara", "email": "[email protected]", "locale": "es",
"api_key" : "995d79acc48864cf8076594c5a9d0d5e97102dc8", "links": [
{"rel": "session" , "uri": "https://facturagem.com/api/session", "methods": "GET, POST, DESTROY"},
{"rel": "account", "uri": "https://facturagem.com/api/accounts/7-daniel-de-la-mata-lara", "methods": "GET, PUT"},
{"rel": "clients", "uri": "https://facturagem.com/api/accounts/7-daniel-de-la-mata-lara/clients", "methods": "GET, POST"},
{"rel": "invoices", "uri": "https://facturagem.com/api/accounts/7-daniel-de-la-mata-lara/invoices", "methods": "GET, POST"},
... ],
... }
And the music was good, and the music was loud
And the music was good, and the music was loud
memoria
success: function(data) {
localStorage.setItem('user_data_api_key', data.user.api_key); localStorage.setItem('user_data_name', data.user.name);
var uri = getUriByRel(data.user.links, 'session'); if(uri){
localStorage.setItem('user_data_session_url', uri); }
uri = getUriByRel(data.user.links, 'account'); if(uri){ localStorage.setItem('user_data_account_url', uri); } ... }