create a mobile web app with sencha touch

101
Create A Mobile Web App with Sencha Touch @ jamespearce

Upload: james-pearce

Post on 15-Nov-2014

11.795 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Create a mobile web app with Sencha Touch

Create A Mobile Web Appwith

Sencha Touch@ jamespearce

Page 2: Create a mobile web app with Sencha Touch

Single deviceSedentary userDeclarativeThin client Documents

Multi deviceMobile userImperative

Thick client Applications

*

* or supine, or sedentary, or passive, or...

Page 3: Create a mobile web app with Sencha Touch

A badge for all these waysthe web is changing

Page 4: Create a mobile web app with Sencha Touch

HTML5 is a new version of HTML4, XHTML1, and DOM Level 2 HTML addressing many of the issues of those specifications while at the same time enhancing (X)HTML

to more adequately address Web applications.

- WHATWG Wiki

Page 5: Create a mobile web app with Sencha Touch

What is an app?

Page 6: Create a mobile web app with Sencha Touch

Consumption vs CreationLinkability

User ExperienceArchitecture

Page 7: Create a mobile web app with Sencha Touch

JavaScript

Semantic HTML

CSS Styling & Layout

WebFont Video Audio Graphics

Workers & Parallel

Processing

File SystemsDatabasesApp Caches

Cross-AppMessaging

Device Access

Camera

Location

Contacts

SMS

Orientation

Gyro

Network

HTTP

AJAX

Events

Sockets

SSL

(all the elements of a modern application platform)

Page 8: Create a mobile web app with Sencha Touch
Page 9: Create a mobile web app with Sencha Touch
Page 10: Create a mobile web app with Sencha Touch

IntroducingSencha Touch

Page 11: Create a mobile web app with Sencha Touch

A JavaScript frameworkfor building

rich mobile appswith web standards

Page 12: Create a mobile web app with Sencha Touch

http://sencha.com/touch

Page 13: Create a mobile web app with Sencha Touch

Components

Lists

Page 14: Create a mobile web app with Sencha Touch

Theming

Page 15: Create a mobile web app with Sencha Touch

Forms

Page 16: Create a mobile web app with Sencha Touch

Scrolling

Page 17: Create a mobile web app with Sencha Touch

Touch Events

Page 18: Create a mobile web app with Sencha Touch

Data access & MVC

Page 19: Create a mobile web app with Sencha Touch

Charts

Page 20: Create a mobile web app with Sencha Touch

Kitchen Sink

http://sencha.com/x/5e

Page 21: Create a mobile web app with Sencha Touch

Hello World

Page 22: Create a mobile web app with Sencha Touch

http://sencha.com/x/d5

Page 23: Create a mobile web app with Sencha Touch
Page 24: Create a mobile web app with Sencha Touch

<!DOCTYPE  html><html>    <head>            <title>Hello  World</title>

       <script  src="lib/touch/sencha-­‐touch.js"></script>        <script  src="app/app.js"></script>                <link  href="lib/touch/resources/css/sencha-­‐touch.css"                      rel="stylesheet"  type="text/css"  />        </head>    <body></body></html>

Page 25: Create a mobile web app with Sencha Touch

new  Ext.Application({

       launch:  function()  {

               new  Ext.Panel({                        fullscreen:  true,                        dockedItems:  [{xtype:'toolbar',  title:'My  First  App'}],                        layout:  'fit',                        styleHtmlContent:  true,                        html:  '<h2>Hello  World!</h2>I  did  it!'                });

       }

});

Page 26: Create a mobile web app with Sencha Touch
Page 27: Create a mobile web app with Sencha Touch

Lists

var  list  =  new  Ext.List({        store:  store,        itemTpl:  '{firstName}  {lastName}',        grouped:  true,        indexBar:  true});

Page 28: Create a mobile web app with Sencha Touch

Nested Lists

var  list  =  new  Ext.NestedList({        store:  store,        displayField:  'name',        title:  'My  List',        updateTitleText:  true,        getDetailCard:                function(record,  parent)  {..}});

Page 29: Create a mobile web app with Sencha Touch

Carousels

var  carousel  =  new  Ext.Carousel({        direction:  'horizontal',        indicator:  true,        items:  [                ..        ]});

Page 30: Create a mobile web app with Sencha Touch

Sheets

var  sheet  =  new  Ext.ActionSheet({        items:  [                {                        text:  'Delete  draft',                        ui:  'decline'                },  {                        text:  'Save  draft'                },  {                        text:  'Cancel',                }        ]});

sheet.show();

Page 31: Create a mobile web app with Sencha Touch

Common patterns1

var  list  =  new  Ext.List({        store:  store,        itemTpl:  '{firstName}  {lastName}',        grouped:  true,        indexBar:  true});

var  panel  =  new  Ext.Panel({        fullscreen:  true,        layout:  'fit',        items:  [list]});

Page 32: Create a mobile web app with Sencha Touch

Common patterns2

var  panel  =  new  Ext.Panel({        fullscreen:  true,        layout:  'fit',        items:  [{                xtype:  'list',                store:  store,                itemTpl:  '{firstName}  {lastName}',                grouped:  true,                indexBar:  true        }]});

Page 33: Create a mobile web app with Sencha Touch

A more complex app

Page 34: Create a mobile web app with Sencha Touch

Pre-requisitesSencha Touch SDK:

  http://sencha.com/products/touch/

 Yelp developer API key:  http://www.yelp.com/developers/getting_started/

api_overview

 Install Sass and Compass:  http://sass-lang.com/download.html

http://compass-style.org/install/

Page 35: Create a mobile web app with Sencha Touch

The Valley App

http://senchalearn.github.com/valleyhttp://sencha.com/x/dr

Page 36: Create a mobile web app with Sencha Touch

https://github.com/senchalearn/valley

Page 37: Create a mobile web app with Sencha Touch

Development sequence

1 Structure the app

2 Layout the UI

3 Model the data

4 Load the list

5 Detail page

6 Add a map

7 More data

8 Customize theme

Page 38: Create a mobile web app with Sencha Touch

1 Structure the app

Page 39: Create a mobile web app with Sencha Touch

index.html

<!doctype  html><html>        <head>                <title>Valley  Guide</title>        </head>

<body></body></html>

Page 40: Create a mobile web app with Sencha Touch

index.html

<script  src="lib/touch/sencha-­‐touch.js"></script>

<script  src="app/yelp.js"></script><script  src="app/app.js"></script>

<link  href="lib/touch/resources/css/sencha-­‐touch.css"              rel="stylesheet"  type="text/css"  />

Page 41: Create a mobile web app with Sencha Touch

app.jsva  =  new  Ext.Application({

       launch:  function()  {

               this.viewport  =  new  Ext.Panel({

                       layout:  'card',                        fullscreen:  true,                        html:  "Hello  world!"                        });

       }

});

Page 42: Create a mobile web app with Sencha Touch

na.viewport

2 Layout the UI

listCard detailCard

toolbar toolbar

dataList

Page 43: Create a mobile web app with Sencha Touch

The app...var  va  =  new  Ext.Application({        launch:  function()  {

               this.listCard  =  new  Ext.Panel({                        html:  'I  am  the  list'                });

               this.detailCard  =  new  Ext.Panel({                        html:  'I  am  the  detail'                });

               this.viewport  =  new  Ext.Panel({                        layout:  'card',                        fullscreen:  true,                        cardSwitchAnimation:  'slide',                        items:  [this.listCard,  this.detailCard]                });        }});

Page 44: Create a mobile web app with Sencha Touch

listCardthis.listCardToolbar  =  new  Ext.Toolbar({        dock:  'top',        title:  'Valley  Guide'});

this.listCardDataList  =  new  Ext.List({        store:  null,        itemTpl:  ''});

this.listCard  =  new  Ext.Panel({        dockedItems:  [this.listCardToolbar],        items:  [this.listCardDataList],        layout:  'fit'});

Page 45: Create a mobile web app with Sencha Touch

detailCardthis.detailCardToolbar  =  new  Ext.Toolbar({        dock:  'top',        title:  '...'});

this.detailCard  =  new  Ext.Panel({        dockedItems:  [this.detailCardToolbar]});

Page 46: Create a mobile web app with Sencha Touch

3 Model the data

http://api.yelp.com/business_review_search?ywsid=YELP_KEY&term=Restaurants&location=Silicon%20Valley

Page 47: Create a mobile web app with Sencha Touch
Page 48: Create a mobile web app with Sencha Touch

Apigee console

Page 49: Create a mobile web app with Sencha Touch

"businesses":  [        {          "rating_img_url"  :  "http://media4.px.yelpcdn.com/...",          "country_code"  :  "US",          "id"  :  "BHpAlynD9dIGIaQDRqHCTA",          "is_closed"  :  false,          "city"  :  "Nashville",          "mobile_url"  :  "http://mobile.yelp.com/biz/...",          "review_count"  :  50,          "zip"  :  "11231",          "state"  :  "TN",          "latitude"  :  40.675758,          "address1"  :  "253  Conover  St",          "address2"  :  "",          "address3"  :  "",          "phone"  :  "7186258211",          "state_code"  :  "TN",          "categories":  [            ...",          ],          ...

Page 50: Create a mobile web app with Sencha Touch

A data namespacethis.data  =  {};

Page 51: Create a mobile web app with Sencha Touch

The ‘Business’ modelthis.data.Business  =  Ext.regModel('',  {        fields:  [                {name:  "id",  type:  "int"},                {name:  "name",  type:  "string"},                {name:  "latitude",  type:  "string"},                {name:  "longitude",  type:  "string"},                {name:  "address1",  type:  "string"},                {name:  "address2",  type:  "string"},                {name:  "address3",  type:  "string"},                {name:  "phone",  type:  "string"},                {name:  "state_code",  type:  "string"},                {name:  "mobile_url",  type:  "string"},                {name:  "rating_img_url_small",  type:  "string"},                {name:  "photo_url",  type:  "string"},        ]});

Page 52: Create a mobile web app with Sencha Touch

A store of those modelsthis.data.restaurants  =  new  Ext.data.Store({        model:  this.data.Business,        autoLoad:  true,        proxy:  {                type:  'scripttag',                url:  'http://api.yelp.com/business_review_search'  +                        '?ywsid='  +  YELP_KEY  +                        '&term=Restaurant'  +                        '&location=Silicon%20Valley',                reader:  {                        type:  'json',                        root:  'businesses'                }        }});

Page 53: Create a mobile web app with Sencha Touch

4 Load the listthis.listCardDataList  =  new  Ext.List({        store:  this.data.restaurants,        itemTpl:  '{name}'});

Page 54: Create a mobile web app with Sencha Touch

A more interesting templateitemTpl:        '<img  class="photo"  src="{photo_url}"  width="40"  height="40"/>'  +        '{name}<br/>'  +        '<img  src="{rating_img_url_small}"/>&nbsp;'  +        '<small>{address1}</small>'

Page 55: Create a mobile web app with Sencha Touch

Hack the style<style>        .photo  {                float:left;                margin:0  8px  16px  0;                border:1px  solid  #ccc;                -­‐webkit-­‐box-­‐shadow:                        0  2px  4px  #777;        }</style>

Page 56: Create a mobile web app with Sencha Touch

Get images resized...

...width="40"  height="40"  />

Page 57: Create a mobile web app with Sencha Touch

...in the cloud

src="http://src.sencha.io/40/{photo_url}"  width="40"  height="40"/>

Page 58: Create a mobile web app with Sencha Touch

5 Detail pagethis.listCardDataList  =  new  Ext.List({        store:  this.data.restaurants,        itemTpl:  ...        listeners:  {                selectionchange:  function  (selectionModel,  records)  {                        if  (records[0])  {                                va.viewport.setActiveItem(va.detailCard);                                va.detailCardToolbar.setTitle(                                        records[0].get('name')                                );                        }                }        }});

Page 59: Create a mobile web app with Sencha Touch

A back buttonthis.detailCardToolbar  =  new  Ext.Toolbar({        dock:  'top',        title:  '...',        items:  [{                text:  'Back',                ui:  'back',                handler:  function  ()  {                        va.viewport.setActiveItem(                                va.listCard,                                {type:  'slide',  direction:  'right'}                        );                }        }]});

Page 60: Create a mobile web app with Sencha Touch

Detail templatethis.detailCard  =  new  Ext.Panel({        dockedItems:  [this.detailCardToolbar],        styleHtmlContent:  true,        cls:  'detail',        tpl:  [                '<img  class="photo"  src="{photo_url}"                            width="100"  height="100"/>',                '<h2>{name}</h2>',                '<div  class="info">',                        '{address1}<br/>',                        '<img  src="{rating_img_url_small}"/>',                '</div>',                '<div  class="phone  x-­‐button">',                        '<a  href="tel:{phone}">{phone}</a>',                '</div>',                '<div  class="link  x-­‐button">',                        '<a  href="{mobile_url}">Read  more</a>',                '</div>'        ]});

Page 61: Create a mobile web app with Sencha Touch

A little styling.x-­‐html  h2  {        margin-­‐bottom:0;}.phone,  .link  {        clear:both;        font-­‐weight:bold;        display:block;        text-­‐align:center;        margin-­‐top:8px;}

Page 62: Create a mobile web app with Sencha Touch

6 Add a map

va.viewport

listCard detailTabs

toolbar toolbar

dataList dataListdetailCard

Page 63: Create a mobile web app with Sencha Touch

6 Add a map

va.viewport.setActiveItem(va.detailTabs);

...

this.detailMap  =  new  Ext.Map({});

this.detailTabs  =  new  Ext.TabPanel({        dockedItems:  [this.detailCardToolbar],        items:  [this.detailCard,  this.detailMap]});

va.viewport  =  new  Ext.Panel({        layout:  'card',        fullscreen:  true,        cardSwitchAnimation:  'slide',        items:  [this.listCard,  this.detailTabs]});

Page 64: Create a mobile web app with Sencha Touch

Tab titles

this.detailCard  =  new  Ext.Panel({        ...        title:  'Info'});

this.detailMap  =  new  Ext.Map({        title:  'Map'});

Page 65: Create a mobile web app with Sencha Touch

Google Maps script

<script  type="text/javascript"    src="http://maps.google.com/maps/api/js?sensor=true"></script>

Page 66: Create a mobile web app with Sencha Touch

Update the map locationselectionchange:  function  (selectionModel,  records)  {        ...        var  map  =  va.detailMap.map;

       if  (!map.marker)  {                map.marker  =  new  google.maps.Marker();                map.marker.setMap(map);        }

       map.setCenter(                new  google.maps.LatLng(                        records[0].get('latitude'),                        records[0].get('longitude')                )        );

       map.marker.setPosition(                map.getCenter()        );

Page 67: Create a mobile web app with Sencha Touch

Improve the tab barthis.detailTabs  =  new  Ext.TabPanel({        dockedItems:  [this.detailCardToolbar],        items:  [this.detailCard,  this.detailMap],

       tabBar:  {                ui:  'light',                layout:  {  pack:  'center'  }        }

});

Page 68: Create a mobile web app with Sencha Touch

7 More?

Page 69: Create a mobile web app with Sencha Touch

More data...['hotels',  'bars',  'restaurants'].forEach(  function  (type)  {        va.data[type]  =  new  Ext.data.Store({                model:  va.data.Business,                autoLoad:  true,                proxy:  {                        type:  'scripttag',                        url:  'http://api.yelp.com/business_review_search'  +                                '?ywsid='  +  YELP_KEY  +                                '&term='  +  type  +                                '&location=Silicon%20Valley',                        reader:  {                                type:  'json',                                root:  'businesses'                        }                }        });});

Page 70: Create a mobile web app with Sencha Touch

Make list into a ‘class’this.ListCardDataList  =  Ext.extend(Ext.List,  {        store:  null,        itemTpl:                '<img  class="photo"  ...

Page 71: Create a mobile web app with Sencha Touch

Instantiate that 3 timesthis.stayCardDataList  =  new  this.ListCardDataList({        store:  this.data.hotels});

this.eatCardDataList  =  new  this.ListCardDataList({        store:  this.data.restaurants});

this.drinkCardDataList  =  new  this.ListCardDataList({        store:  this.data.bars});

Consider lazy-loading...

Page 72: Create a mobile web app with Sencha Touch

Turn container into tabs toothis.listCard  =  new  Ext.TabPanel({        items:  [                this.stayCardDataList,                  this.eatCardDataList,                  this.drinkCardDataList        ],        tabBar:  {                ui:  'light',                layout:  {  pack:  'center'  },                dock:  'bottom'        },        cardSwitchAnimation:  'flip',...

Page 73: Create a mobile web app with Sencha Touch

And add titles & iconsthis.stayCardDataList  =  new  this.ListCardDataList({        store:  this.data.hotels,        title:  'Stay',        iconCls:  'home'});

this.eatCardDataList  =  new  this.ListCardDataList({        store:  this.data.restaurants,        title:  'Eat',        iconCls:  'locate'});

this.drinkCardDataList  =  new  this.ListCardDataList({        store:  this.data.bars,        title:  'Drink',        iconCls:  'star'});

Page 74: Create a mobile web app with Sencha Touch

Pull-to-refreshthis.ListCardDataList  =  Ext.extend(Ext.List,  {        ...        plugins:  [{                ptype:  'pullrefresh'        }]});

Page 75: Create a mobile web app with Sencha Touch

8 Customize theme

Page 76: Create a mobile web app with Sencha Touch

http://sass-lang.com/

Page 77: Create a mobile web app with Sencha Touch

/* SCSS */

$blue: #3bbfce;$margin: 16px;

.content-navigation { border-color: $blue; color: darken($blue, 9%);}

.border { padding: $margin / 2; margin: $margin / 2; border-color: $blue;}

/* CSS */

.content-navigation { border-color: #3bbfce; color: #2b9eab;}

.border { padding: 8px; margin: 8px; border-color: #3bbfce;}

Variables

Page 78: Create a mobile web app with Sencha Touch

$> sudo gem install compass

http://rubyinstaller.org/

Page 79: Create a mobile web app with Sencha Touch

$> compass -v

Compass 0.11.1 (Antares)Copyright (c) 2008-2011 Chris EppsteinReleased under the MIT License.

$> sass -v

Sass 3.1.1 (Brainy Betty)

Page 80: Create a mobile web app with Sencha Touch

Start by copying sencha-touch.scss

Page 81: Create a mobile web app with Sencha Touch

config.rbdir  =  File.dirname(__FILE__)

load  File.join(dir,  '..',  'lib',  'touch',  'resources',  'themes')

#  Compass  configurationssass_path        =  dircss_path          =  direnvironment    =  :productionoutput_style  =  :compressed

#  or  :nested,  :expanded,  :compact

Page 82: Create a mobile web app with Sencha Touch

Compile...$>  cd  theming

$>  compass  compile  valley.scss            create  valley.css

$>  compass  compile  valley.scss            identical  valley.css

[edit  file]$>  compass  compile  valley.scss            overwrite  valley.css

$>  compass  watch  valley.scss            >>>  Change  detected  to:  valley.scss            overwrite  valley.css

Page 83: Create a mobile web app with Sencha Touch

Link...<link  href="theming/valley.css"  rel="stylesheet"            type="text/css"  />

Page 84: Create a mobile web app with Sencha Touch

valley.scss@import  'sencha-­‐touch/default/all';

@include  sencha-­‐panel;@include  sencha-­‐buttons;@include  sencha-­‐sheet;@include  sencha-­‐tabs;@include  sencha-­‐toolbar;@include  sencha-­‐list;@include  sencha-­‐list-­‐pullrefresh;@include  sencha-­‐layout;@include  sencha-­‐loading-­‐spinner;...

Page 85: Create a mobile web app with Sencha Touch

valley.scss$base-­‐color:  #9D9E00;

@import  'sencha-­‐touch/default/all';

@include  sencha-­‐panel;@include  sencha-­‐buttons;@include  sencha-­‐sheet;@include  sencha-­‐tabs;@include  sencha-­‐toolbar;@include  sencha-­‐list;@include  sencha-­‐list-­‐pullrefresh;@include  sencha-­‐layout;@include  sencha-­‐loading-­‐spinner;

Page 86: Create a mobile web app with Sencha Touch
Page 87: Create a mobile web app with Sencha Touch

Choose own icons$base-­‐color:  #9D9E00;$include-­‐default-­‐icons:  false;

@import  'sencha-­‐touch/default/all';

@include  sencha-­‐panel;@include  sencha-­‐buttons;...

@include  pictos-­‐iconmask('briefcase1');@include  pictos-­‐iconmask('heart');@include  pictos-­‐iconmask('music1');

Page 88: Create a mobile web app with Sencha Touch

Specify iconClsthis.stayCardDataList  =  new  this.ListCardDataList({        store:  this.data.hotels,        title:  'Stay',        iconCls:  'briefcase1'});

this.eatCardDataList  =  new  this.ListCardDataList({        store:  this.data.restaurants,        title:  'Eat',        iconCls:  'heart'});

this.drinkCardDataList  =  new  this.ListCardDataList({        store:  this.data.bars,        title:  'Drink',        iconCls:  'music1'});

Page 89: Create a mobile web app with Sencha Touch

_variables.scss

$include-html-style: true;

$include-default-icons: true;

$include-form-sliders: true;

$include-floating-panels: true;

$include-default-uis: true;

$include-highlights: true;

$include-border-radius: true;

$basic-slider: false;

$base-color: #354F6E;

$base-gradient: 'matte';

$alert-color: red;

$confirm-color: #92cf00;

$page-bg-color: #eee;

$global-row-height: 2.6em;

$active-color: darken( saturate($base-color, 55%), 10%);

Page 90: Create a mobile web app with Sencha Touch

http://dev.sencha.com/deploy/touch/docs/theme/

Page 91: Create a mobile web app with Sencha Touch

Sass is a superset of CSS$base-­‐color:  #9D9E00;$include-­‐default-­‐icons:  false;

@import  'sencha-­‐touch/default/all';...

@include  pictos-­‐iconmask('briefcase1');@include  pictos-­‐iconmask('heart');@include  pictos-­‐iconmask('music1');

.photo  {        float:left;        margin:0  8px  16px  0;        border:1px  solid  #ccc;        -­‐webkit-­‐box-­‐shadow:                0  2px  4px  #777;}...

Page 92: Create a mobile web app with Sencha Touch

WebFonts@import  url(http://fonts.googleapis.com/css?family=Voltaire);

.x-­‐toolbar-­‐title  {    font-­‐family:  Voltaire;    font-­‐weight:  normal;    font-­‐size:  1.7em;    line-­‐height:  1.7em;    letter-­‐spacing:  0.05em;}

Page 93: Create a mobile web app with Sencha Touch

Done?

Page 94: Create a mobile web app with Sencha Touch

Development sequence

1 Structure the app

2 Layout the UI

3 Model the data

4 Load the list

5 Detail page

6 Add a map

7 More data

8 Customize theme

Page 95: Create a mobile web app with Sencha Touch

A ‘responsive’ app...

http://sencha.com/x/cv

Page 96: Create a mobile web app with Sencha Touch

And if we’d had time...

Add to home screen - Icon - Splash screen

Hybrid app; PhoneGap / NimbleKit - Contacts API - Geolocation

http://sencha.com/x/cyhttp://sencha.com/x/de

Page 97: Create a mobile web app with Sencha Touch

O!ine app$>  phantomjs  confess.js  http://github/valley/

CACHE  MANIFEST

#  This  manifest  was  created  by  confess.js#                    Time:  Wed  Sep  14  2011  10:14:45  GMT-­‐0700  (PDT)#        User-­‐agent:  Mozilla/5.0  ...

CACHE:app/app.jsapp/yelp.jshttp://cdn.sencha.io/touch/1.1.0/sencha-­‐touch.jshttp://maps.google.com/maps/api/js?sensor=truehttp://maps.gstatic.com/intl/en_us/mapfiles/api-­‐3/6/4/main.jstheming/valley.css

NETWORK:*

http://github.com/jamesgpearce/confess

Page 98: Create a mobile web app with Sencha Touch

O!ine data

Taking Yelp data o!ine

Taking images o!ine - src.sencha.io to generate cross-origin B64

Detecting network connection changes

http://sencha.com/x/df

Page 99: Create a mobile web app with Sencha Touch

Weinre

http://phonegap.github.com/weinre

Page 100: Create a mobile web app with Sencha Touch

Apps vs Web technologybuilt with

Page 101: Create a mobile web app with Sencha Touch

James Pearce @ jamespearce