jQuery Mobile Deep Dive

71
jQuery Mobile Deep Dive 7 September 2013 Culver City, California USA Saturday, September 7, 13

description

jQuery Mobile Deep Dive shows you how to create single page web apps that look and behave like native ones. We use JavaScript and open source libraries like jQuery, underscore, and backbone. And explore advanced topics like unit testing and performance optimization.

Transcript of jQuery Mobile Deep Dive

Page 1: jQuery Mobile Deep Dive

jQuery MobileDeep Dive

7 September 2013Culver City, California USA

Saturday, September 7, 13

Page 2: jQuery Mobile Deep Dive

Google MapsFull Throttle

• Learn the tips and tricks of the world’s premier mapping app

• 21 September 2013

• Irvine, California USA

• http://bit.ly/156Mr5C

Saturday, September 7, 13

Page 3: jQuery Mobile Deep Dive

Want more? Follow me for more tutorials and source code.@therockncoder

Saturday, September 7, 13

Page 4: jQuery Mobile Deep Dive

Source code for my tutorials hosted on GitHub @

https://github.com/Rockncoder

Saturday, September 7, 13

Page 5: jQuery Mobile Deep Dive

Before We Begin

• This class will move fast

• The source code is yours to keep

• The slides are available for download

Saturday, September 7, 13

Page 6: jQuery Mobile Deep Dive

What Are We Going To Do?

We are going to learn about jQuery Mobile by examining a working application. We aren’t going to decompose each line of code, instead we will examine each major area of

functionality.

Saturday, September 7, 13

Page 7: jQuery Mobile Deep Dive

Coffee Coffee+

• jQuery Mobile

• Backbone

• Responsive Design

• Templates

Saturday, September 7, 13

Page 8: jQuery Mobile Deep Dive

jQuery &jQuery Mobile

• jQuery 1.9.x

• jQuery Mobile 1.3.2

• released on July 19th

• jQuery Mobile 1.4 which will support jQuery 2.0, coming soon

Saturday, September 7, 13

Page 9: jQuery Mobile Deep Dive

Memory Management

Saturday, September 7, 13

Page 10: jQuery Mobile Deep Dive

Memory Management Tips

• Globals are bad

• Use functions for information hiding

Saturday, September 7, 13

Page 11: jQuery Mobile Deep Dive

Globals are bad

• Try to use only one global variable

• Remember: JS objects can be modified at runtime

• Use var app = app || {};

Saturday, September 7, 13

Page 12: jQuery Mobile Deep Dive

Information Hiding

• JS lacks classes or other information

• One alternative is to wrap code in anonymous functions

Saturday, September 7, 13

Page 13: jQuery Mobile Deep Dive

Performance Optimization

Saturday, September 7, 13

Page 14: jQuery Mobile Deep Dive

The fastest code is the code which is never

run.

Saturday, September 7, 13

Page 15: jQuery Mobile Deep Dive

Performance Optimization

• JavaScript

• jQuery

• HTML

Saturday, September 7, 13

Page 16: jQuery Mobile Deep Dive

JavaScript

• Define local variables

• Be careful with closures

• Accessing object properties and array items is slower than variables

• Avoid for-in loops

Saturday, September 7, 13

Page 17: jQuery Mobile Deep Dive

jQuery

• Don’t use .live()

• Use $.mobile.activePage

• Use preventDefault()

• Cache selector

• Know when not to use jQuery

Saturday, September 7, 13

Page 18: jQuery Mobile Deep Dive

HTML

• Copy HTML collections into JS arrays

• Avoid accessing the DOM

• Use CSS classes not styles

Saturday, September 7, 13

Page 19: jQuery Mobile Deep Dive

Templates

Saturday, September 7, 13

Page 20: jQuery Mobile Deep Dive

Templates

• Think of a template like a cookie cutter

• Fill it with data, and it produces HTML

• Templates can be used on the server or on the client

• There are many templating engines

Saturday, September 7, 13

Page 21: jQuery Mobile Deep Dive

Templating Engines

• Underscore

• Handlebars

• Jade

• dust (LinkedIn)

• http://garann.github.io/template-chooser/

Saturday, September 7, 13

Page 22: jQuery Mobile Deep Dive

Handlebars

• Create a template in a script tag

• Compile template in JS

• Render template with data

Saturday, September 7, 13

Page 23: jQuery Mobile Deep Dive

Looping

• Handlebars has a built helper named, each, for looping

• else can be used with each for when no data is returned

• @index is the current loop index

• @key is the current object

Saturday, September 7, 13

Page 24: jQuery Mobile Deep Dive

Conditional

• The if helper conditionally renders a block

• else is available for falsy values

• unless inverses the logic of the if

Saturday, September 7, 13

Page 25: jQuery Mobile Deep Dive

Pre-compilation

• Precompile templates at application startup for improved performance later

• Can also ship precompiled templates using command line, node based tools

Saturday, September 7, 13

Page 26: jQuery Mobile Deep Dive

Helpers

• Handlebars allows for the creation of JS helper functions

• These handlers make it possible to extend Handlebars functionality

Saturday, September 7, 13

Page 27: jQuery Mobile Deep Dive

MV* Frameworks

Saturday, September 7, 13

Page 28: jQuery Mobile Deep Dive

MV* Frameworks

• MV* Frameworks give an application structure

• It is very easy to create spaghetti code in JS

• M = model

• V = controller

• * = something else, like routing

Saturday, September 7, 13

Page 29: jQuery Mobile Deep Dive

MV* & jQuery Mobile

• jQuery Mobile and most frameworks don’t get along well

• The main reason is routing. Both JQM and MV* want to control the routes

Saturday, September 7, 13

Page 30: jQuery Mobile Deep Dive

URL Routing

• URL are an invaluable part of the internet

• Users are use to sharing them

• They provide an easy mechanism to save state in an otherwise stateless environment

Saturday, September 7, 13

Page 31: jQuery Mobile Deep Dive

Routing

• It is better for the MV* to handle routing

• To stop JQM from handling routing

•26 <script type="text/javascript"> 27 /* Prevent jQM from handling <a> clicks and # changes */ 28 /* IMPORTANT: This must be hooked before loading jQM */ 29 $(document).on("mobileinit", function () { 30 $.mobile.linkBindingEnabled = false; 31 $.mobile.hashListeningEnabled = false; 32 }); 33 </script> 34 <script src="js/libs/jquery.mobile-1.3.2.min.js" type="text/javascript"></script>

Saturday, September 7, 13

Page 32: jQuery Mobile Deep Dive

MV* Frameworks

• Angular.js, from Google

• Ember.js, based on SproutCore

• Backbone.js,

• and many more

• http://todomvc.com/

Saturday, September 7, 13

Page 33: jQuery Mobile Deep Dive

Backbone.js

• A lightweight JS library which ads structure to your client side code

• From the creator of Underscore

• Very small - 6.3k minified and gzipped

Saturday, September 7, 13

Page 34: jQuery Mobile Deep Dive

Small and Fast

• Ember.js = 56k minified

• Angular.js = 81kb minified

• http://jsperf.com/angular-vs-knockout-vs-ember/173

Saturday, September 7, 13

Page 35: jQuery Mobile Deep Dive

The Nickel Tour

• Models

• Collections

• Views

• Routes

Saturday, September 7, 13

Page 36: jQuery Mobile Deep Dive

The Happy Path

• Use extend to create a “class”

• Use built in methods like initialize

• Once the “class” is defined, use new to instantiate an object

• “Classes” begins with upper case

• Objects begins with lower case

Saturday, September 7, 13

Page 37: jQuery Mobile Deep Dive

Models

• Backbone.Model.extend()

• defaults allows for the creation of default values

Saturday, September 7, 13

Page 38: jQuery Mobile Deep Dive

Collections

• Backbone.Collection.extend()

• Use model to define the model of the collection

• Use url to define a restful api

Saturday, September 7, 13

Page 39: jQuery Mobile Deep Dive

Views

• Backbone.View.extend()

• Use the initialize method run handler code which is executed once, like compiling templates

• Use the render method to display the view

Saturday, September 7, 13

Page 40: jQuery Mobile Deep Dive

Routes

• Backbone.Router.extend()

• Normally only one route per app

• Backbone.history.Start() tells BB to begin monitoring the URL

Saturday, September 7, 13

Page 41: jQuery Mobile Deep Dive

Unit Testing

Saturday, September 7, 13

Page 42: jQuery Mobile Deep Dive

Unit Testing

• Many unit testing frameworks are available

• QUnit, from the jQuery Team

• YUITest, from Yahoo

• Jasmine, from Pivotal Labs

• Mocha

Saturday, September 7, 13

Page 43: jQuery Mobile Deep Dive

Unit Testing Tips

• Be sure to test logic

• Write tests before fixing bugs

• Never stop running test

• Refactor the tests, when your refactor the code

Saturday, September 7, 13

Page 44: jQuery Mobile Deep Dive

QUnit

• Probably the easiest one to learn

• Used to test all of the jQuery projects

Saturday, September 7, 13

Page 45: jQuery Mobile Deep Dive

Custom Attributes

Saturday, September 7, 13

Page 46: jQuery Mobile Deep Dive

Custom Attributes

• A custom attribute is a non-standard attribute added to a tag to convey some information

• HTML has always supported custom attributes

• HTML5 makes the support explicit instead of implicit

Saturday, September 7, 13

Page 47: jQuery Mobile Deep Dive

data-*

• The official way of creating custom attributes is with data-

• Recommended is: data-<company initials>-<attribute name>

• example: data-rnc-listingId

Saturday, September 7, 13

Page 48: jQuery Mobile Deep Dive

getNamedItem()

• HTML, aka the DOM, includes the getNamedItem() method

• Returns value of the attribute node with the specified name

• /* set a click event on each row */ $(".listing").off().on("click", function () { app.CurrentListing = this.attributes.getNamedItem("data-rnc-listingid").value; });

Saturday, September 7, 13

Page 49: jQuery Mobile Deep Dive

The Viewport

Saturday, September 7, 13

Page 50: jQuery Mobile Deep Dive

Meta tags

• meta tag is an element which is used to provide structured metadata about a web page

• They can be used to specify page description, keywords, and instructions

• Always in the head section

• 5 defined attributes

Saturday, September 7, 13

Page 51: jQuery Mobile Deep Dive

Meta Tag Attributes

• name, the name of the meta tag

• content, information for the reader

• http-equiv, character encoding, auto refresh

• scheme, specifies scheme to interpret content (not support in HTML5)

• charset, added in HTML5

Saturday, September 7, 13

Page 52: jQuery Mobile Deep Dive

Examples <title>CC+</title> <meta charset="utf-8"/> <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no, maximum-scale=1"/> <meta name="apple-mobile-web-app-capable" content="yes"/> <meta name="robots" content="noindex, nofollow">

Saturday, September 7, 13

Page 53: jQuery Mobile Deep Dive

Viewport

• A special meta tag used to provide information about the device's display

• <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no,maximum-scale=1"/>

Saturday, September 7, 13

Page 54: jQuery Mobile Deep Dive

Viewport Parameters

• width, the width of the viewport in pixels

• initial-scale, the zoom level when the page loads

• maximum-scale, the max zoom level

• minimum-scale, the min zoom level

• user-scalable, if the user can change zoom

Saturday, September 7, 13

Page 55: jQuery Mobile Deep Dive

Tips and Tricks

• There is no standard covering the viewport

• Apple probably has the best docs

• Type carefully, no error checking on viewport

• Use the viewport standardize your app’s screen size across different devices

Saturday, September 7, 13

Page 56: jQuery Mobile Deep Dive

Responsive Design

Saturday, September 7, 13

Page 57: jQuery Mobile Deep Dive

Media Queries

• The workhorse of responsive design

• A media query is a media type and at least one expression that limits the style sheets' scope

@media screen and (min-width: 480px) and (orientation:portrait) {}

Saturday, September 7, 13

Page 58: jQuery Mobile Deep Dive

Media Query by the Piece

• The media query begins with @media

• then a media type, like screen, all, print, braille, etc

• You can add logical operators like: and, not, and only

• There is also the comma separated list which behaves like an or operator

Saturday, September 7, 13

Page 59: jQuery Mobile Deep Dive

jQuery Mobile Features

• Grids, self sizing columns which dynamically resize themselves when the size of the page changes

• Tables, added with version 1.3.0

• Panels, ala Facebook, a panel, usually options, reveals itself when an icon is clicked

Saturday, September 7, 13

Page 60: jQuery Mobile Deep Dive

Minification

Saturday, September 7, 13

Page 61: jQuery Mobile Deep Dive

Why?

Developers using other languages rarely think of the size of their code. But client side JavaScript is different. Minifying JavaScript can dramatically reduce its size and radically reduce page load time.

Saturday, September 7, 13

Page 62: jQuery Mobile Deep Dive

How to Minify

• By Hand

• Using an app

Saturday, September 7, 13

Page 63: jQuery Mobile Deep Dive

By Hand

Saturday, September 7, 13

Page 65: jQuery Mobile Deep Dive

In Practice

• Hand minification is fraught with danger - code is hard to read and grows increasingly fragile

• Using a web site is good for an example but not in practice, too much labor

• Usually done via a command line tool, during build or on page request

Saturday, September 7, 13

Page 66: jQuery Mobile Deep Dive

Icons and Splash Pages

Saturday, September 7, 13

Page 67: jQuery Mobile Deep Dive

Icons

• Classic icon (Microsoft)

• Standardized

• Mobile icon (Apple)

Saturday, September 7, 13

Page 68: jQuery Mobile Deep Dive

Splash Page

• An Apple thing

• The splash page is cached on the user’s device

• When the icon is click on the home page, splash page is shown while the app is loaded

Saturday, September 7, 13

Page 69: jQuery Mobile Deep Dive

HTML5 Offline App

Saturday, September 7, 13

Page 70: jQuery Mobile Deep Dive

How to Reach Me

[email protected]

• http://therockncoder.blogspot.com/

• https://github.com/Rockncoder/ccplus

Saturday, September 7, 13

Page 71: jQuery Mobile Deep Dive

Questions

Saturday, September 7, 13