Transcript
Page 1: Angular js mobile   jsday 2014 - Verona 14 may

ANGULARJS MOBILE- VERONA 14 MAYJSDAY 2014

by Luciano Amodio

:

:

adamquadmon

@lucianoamodio

Page 2: Angular js mobile   jsday 2014 - Verona 14 may

WHO AM I?

Page 3: Angular js mobile   jsday 2014 - Verona 14 may

I'M A SOFTWARE ENGINEER

DOING CODE SINCE 2000

Page 4: Angular js mobile   jsday 2014 - Verona 14 may

MY FIRST CONFERENCE (AS AUDITOR)

HERE IN VERONA IN 2012

Page 5: Angular js mobile   jsday 2014 - Verona 14 may

THE SECOND ONE

MARCH 2013

Page 6: Angular js mobile   jsday 2014 - Verona 14 may

HERE I MEET

AND WAS LOVE!

Page 7: Angular js mobile   jsday 2014 - Verona 14 may

I STARTED STUDYING HARD

Page 8: Angular js mobile   jsday 2014 - Verona 14 may

DOING IT WRONG

generator-angular-silex

Page 10: Angular js mobile   jsday 2014 - Verona 14 may

THEN I WAS INVOLVED IN

Page 11: Angular js mobile   jsday 2014 - Verona 14 may

AND WITH AN INSPIRING LEADAND AN AMAZING TEAM

Page 12: Angular js mobile   jsday 2014 - Verona 14 may

WE DID IT!

THE ANGULARJS VERSIONOF THE NAMSHI MOBILE WEBSITE

Page 13: Angular js mobile   jsday 2014 - Verona 14 may

IN THIS TALK I WANT TO SHARE WITH YOUHOW WE MADE ROCKET INTERNET IN GERMANY SAY

WOW!

Page 14: Angular js mobile   jsday 2014 - Verona 14 may

LET'S START

Page 15: Angular js mobile   jsday 2014 - Verona 14 may

WHAT IS

RESPONSIVE?

Page 16: Angular js mobile   jsday 2014 - Verona 14 may

a responsive website respond to the ambient adapting itself toprovide the best user experience

Page 17: Angular js mobile   jsday 2014 - Verona 14 may

WHAT CAN BE RESPONSIVE?

Page 18: Angular js mobile   jsday 2014 - Verona 14 may

LAYOUT

Page 19: Angular js mobile   jsday 2014 - Verona 14 may

LAYOUT / MEDIA QUERIES

Media queries are filters that can be applied to CSS styles.

They make it easy to change styles based on the device, includingthe display type, width, height, orientation and even resolution.

Page 20: Angular js mobile   jsday 2014 - Verona 14 may

LAYOUT / SET YOUR VIEWPORT

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

Page 21: Angular js mobile   jsday 2014 - Verona 14 may

LAYOUT / TECNIQUES

Google Web Fundamentals

Page 22: Angular js mobile   jsday 2014 - Verona 14 may

LAYOUT / MOSTLY FLUYD

Page 23: Angular js mobile   jsday 2014 - Verona 14 may

LAYOUT / COLUMN DROP

Page 24: Angular js mobile   jsday 2014 - Verona 14 may

LAYOUT / LAYOUT SHIFTER

Page 25: Angular js mobile   jsday 2014 - Verona 14 may

LAYOUT / OFF CANVAS

Page 26: Angular js mobile   jsday 2014 - Verona 14 may

IMAGES

Page 27: Angular js mobile   jsday 2014 - Verona 14 may

IMAGES / OPTIMIZATION

You should

maybe with

Optimize Images

grunt-responsive-image

Page 28: Angular js mobile   jsday 2014 - Verona 14 may

IMAGES / RESPONSIVE

Should I use srcset attribute or picture element?

Try ask those guys:

Maybe using for a picture element polyfill

or a BBC solution:

<img src="fallback.jpg" srcset="photo.jpg 1x, photo-hd.jpg 2x">

<picture> <source media="(min-width: 40em)" srcset="big.jpg 1x, big-hd.jpg 2x"> <source srcset="small.jpg 1x, small-hd.jpg 2x"> <img src="fallback.jpg"></picture>

responsiveimages.org

picturefill

Images.js

Page 29: Angular js mobile   jsday 2014 - Verona 14 may

IMAGES / RESPONSIVE

our solution (the concept):

<img ng-src="//ourCdn.tld/imageName-{{deviceSize}}.jpg">

Page 30: Angular js mobile   jsday 2014 - Verona 14 may

FEATURES

You can use to provide different content orbehaviours (think about mouseover on a touch only device!)

Modernizr

But don't forget to provied (expecially for IE)polyfills

Page 31: Angular js mobile   jsday 2014 - Verona 14 may

USEFUL RESOURCES

You really should read Google Web Fundamentals

But also giving a look at is not a badidea!

Yeoman generator-mobile

Page 32: Angular js mobile   jsday 2014 - Verona 14 may

MOBILE ISSUES

Page 33: Angular js mobile   jsday 2014 - Verona 14 may
Page 34: Angular js mobile   jsday 2014 - Verona 14 may

DON'T MAKE THEM WAIT!

PRETEND TO BE FAST

Page 35: Angular js mobile   jsday 2014 - Verona 14 may

NGCLOAK

<style> [ng-cloak].splash { display: block !important; } [ng-cloak] {display: none;} .splash { background-color: blue; }</style><div class="splash" ng-cloak=""></div>

Page 36: Angular js mobile   jsday 2014 - Verona 14 may

NGCLOAK

<style> [ng-cloak].splash { display: block !important; } [ng-cloak] {display: none;} .splash { background-color: blue; }</style><div class="splash" ng-cloak=""></div>

AngularJs ngCloak directive will remove the ng-cloakattribute once theapplication is ready.

Instead of hiding the underlying application (this is the standard use of ngCloak),this tecnique will show an overlying splash screen as soon as the first part of theresponse is served.

It's very important to serve the spalsh screen as soon as possible (deferringexternal resources, embedding needed CSS in the document itself and so on)

Page 37: Angular js mobile   jsday 2014 - Verona 14 may

UX INSTANT FEEDBACK

For a mobile user is not big difference using a website or an app.

Provide the UX with visual/audio feedback and loading routines will make theuser feel the application is responsive to her input.

it's a library that help you add audio events to UI.

For loaders & spinners give a look to:

Howler.js

the state of the spinner

SpinKit

Page 38: Angular js mobile   jsday 2014 - Verona 14 may

REDUCE LATENCY

AVOID HTTP CALLS

Page 39: Angular js mobile   jsday 2014 - Verona 14 may

REDUCE LATENCY

AVOID HTTP CALLS WHILE LOADING THE PAGEConcatenate and minify html, CSS and JS files.( and don't need this)

Inline images in css ( or )

Use a single sprite for CSS UI ( )

Defer every not critical resource after the app is ready (social media plugin asfacebook, twitter, etc):

HTTP2 Spdy

grunt-base64 grunt-data-uri

glue

if(document.readyState === 'complete') { //your code}

angular.run(function() { //your code});

Page 40: Angular js mobile   jsday 2014 - Verona 14 may

REDUCE LATENCY

AVOID HTTP CALLSPaginate results.

If you are displaying a catalog page you can embed a small json with a bounce ofproducts in the document itself, and then ask the API for next resources.

If your json is not that big you can also serve a large number of items and show onthe client only a bounce of them at the same time.

Paginate client side will give a user the feeling of a super fast application.

Page 41: Angular js mobile   jsday 2014 - Verona 14 may

REDUCE LATENCY

WITH ANGULARJSAngularJs provide an internal cache service you can use to avoid multiplerequests of the same resource.It's used, for example, to preload templates with .

is a module to enhance the angular cache sistem.

Batching network requests to fetch data at the same time it's also good to savebattery.

grunt-angular-templates

angular-cache

Page 42: Angular js mobile   jsday 2014 - Verona 14 may

CSS PERFORMANCES

SELECTORSCSS selectors are read right to left

This mean you should avoid unnecessary ones

Nested selectors are slow, even if more verbose is better to avoid them

Yes, sad to say: you should avoid CSS3 selectors!

ul li.error { color: #fff; }

li.error { color: #fff; }

.li-error { color: #fff; }

Page 43: Angular js mobile   jsday 2014 - Verona 14 may

CSS PERFORMANCES

SELECTORS BY PERFORMANCESID #header

Class .header

Tag div

Sibling div + p, div ~ p

Child div > p

Descendant div p

Universal div *

Attribute input[type="text"]

Pseudo p:first-child

Page 44: Angular js mobile   jsday 2014 - Verona 14 may

CSS PERFORMANCES

ANIMATIONSAvoid css animations

At least avoid gradients and similar expensive stuff (you can use sprites).

Page 45: Angular js mobile   jsday 2014 - Verona 14 may

CSS PERFORMANCES

CSS VS JS ANIMATION?

Someone says Js it'slow.

Well, it's not JS slow, jQuery is!

Try GASP

Page 46: Angular js mobile   jsday 2014 - Verona 14 may

JAVASCRIPT OPTIMIZATION

This is a long talk, but let me give you a quick tip: (well, angulartoo!).

I strongly suggest you a couple of books: byN.Zakas and by S. Souders.

it's a very rich resource on how to profile (usingChrome Dev Tools) and optimize.

use jQuery wisely

High Performance JavaScriptEven Faster Web Sites

Google Performance Tips

Page 47: Angular js mobile   jsday 2014 - Verona 14 may

THE CURSE OF 300MS

Mobile browsers will wait approximately 300ms from the time that you tapsomething before firing the click event.

The reason for this is that the browser is waiting to see if you are actuallyperforming a double tap.

What to do?

Use FastClick

Page 48: Angular js mobile   jsday 2014 - Verona 14 may

YOU SHOULD TEST

Test always with real devices (even old ones).

Page 49: Angular js mobile   jsday 2014 - Verona 14 may

AND DEBUG

You can use safari remote debugging for iOSor chrome remote debugging + for Androidangularjs-batarang

Page 50: Angular js mobile   jsday 2014 - Verona 14 may

AUTOMATE TESTING

Automate tests with andProtractor Karma

Page 51: Angular js mobile   jsday 2014 - Verona 14 may

AUTOMATE DELIVERY

Go CI or CD (continuous integration/deploy/delivery)with orSauceLabs BrowserStack

Page 52: Angular js mobile   jsday 2014 - Verona 14 may

ANGULARJS

Page 53: Angular js mobile   jsday 2014 - Verona 14 may

WHY ANGULARJS?

Page 54: Angular js mobile   jsday 2014 - Verona 14 may

Because it's awesome!

It enhance HTML, CSS and JavaScriptand it's google and have a huge community:

Page 55: Angular js mobile   jsday 2014 - Verona 14 may

Well, this is also a good reason to hate AngularJs:

You have ruined Javascript.

Page 56: Angular js mobile   jsday 2014 - Verona 14 may

WHAT IS ANGULAR?

Page 57: Angular js mobile   jsday 2014 - Verona 14 may

THE BEST PART

Less code well organized

DI

Testability

Directives and reusability

Promises

Model View Whatever

Whatever -> $scope <- View

Services

Page 58: Angular js mobile   jsday 2014 - Verona 14 may

THE BEST PART*angular.module('myModule').controller('myController', function($scope, MyService) {

$scope.myData = { some: 'data' }; $scope.sendForm = function(){ MyService.performAction($scope.myData) .then( function(newData) { $scope.newData = newData; }) .catch( function(error) { $scope.errorMessage = error; }) .finally( function() { $scope.releaseKraken = true; }); };});

* the use of 'catch' and 'finally' willuse array notation instead: MyService.performAction($scope.myData).['catch']( ... )

break IE8

Page 59: Angular js mobile   jsday 2014 - Verona 14 may

WHY IS ANGULAR STILL IMATURE?still a WIP (a very fast one)

Page 60: Angular js mobile   jsday 2014 - Verona 14 may

WHY IS ANGULAR STILL IMATURE?

Still not a perfect knowledge about it

Services, Factories, ServiceProviders: "it's a mess!"

Page 61: Angular js mobile   jsday 2014 - Verona 14 may

THE FUTURE

Angular 1.3: drop IE8 support

Angular 2.0: use ES6, better DI, better modularity (I hope so!)

Page 62: Angular js mobile   jsday 2014 - Verona 14 may

ANGULAR OPTIMIZATIONS

Maybe you don't really need Angularize everything!

Page 63: Angular js mobile   jsday 2014 - Verona 14 may

ANGULAR OPTIMIZATIONS

Avoid ngRepeat if not needed, this will reduce watchers and save CPU.

Page 64: Angular js mobile   jsday 2014 - Verona 14 may

ANGULAR OPTIMIZATIONS

Don't use filters in views (will be fixed in next versions).

Page 65: Angular js mobile   jsday 2014 - Verona 14 may

ANGULAR OPTIMIZATIONS

Watch the number of others watches (use )bindonce

Page 66: Angular js mobile   jsday 2014 - Verona 14 may

ANGULAR OPTIMIZATIONS

Release resources when you leave a scope

(for example when you change view or remove a directive)

in particular remember to remove listeners.

$scope.$on('$destroy', function(){ // to release resources});

Page 67: Angular js mobile   jsday 2014 - Verona 14 may

ANGULAR FRIENDS

There are some modules to try out:

: to implement hammerJs gestures

: a simple touch carusel

: to snap everything in AngularJs

And don't forget you can still implement simple JS libraries like:or

angular-gestures

angular-carousel

angular-snap

swipeJs hammerJs

Page 68: Angular js mobile   jsday 2014 - Verona 14 may

WHY NOT NGTOUCH?

Remember the course of 300ms?

Well, it only binds ngClick.

Then? Guess what! Use FastClick!

or Ionic...

Page 69: Angular js mobile   jsday 2014 - Verona 14 may

STORAGE

Mobile device can easily lose connection.

You can use localStorageto cache the needed data to make your website reactmore like a native app.

localStoragehave some issues on Safari, you can fallback to cookies.

If you have cross domain cookie problems try to give a look at It's adeprecated library we have used to share cross domain secured cookies.

If your project is an hybrid app, you can also use sqlLite or other dbs.

xAuth.

Page 70: Angular js mobile   jsday 2014 - Verona 14 may

HYBRID APPS

An "Hybrid App" is an App build in HTML, CSS and JS that you can put in an AppStore.

Dealing with Html5 in a webView we can reuse our code

for both iOS AppStore

or Google Play Store

Page 71: Angular js mobile   jsday 2014 - Verona 14 may

WHAT IS PHONEGAP / CORDOVA?

Fills the gap between browsers and native device features

(geo-location, camera, accelerometers, push notificatuons).

Page 72: Angular js mobile   jsday 2014 - Verona 14 may

WHY PHONEGAP / CORDOVA?

It's Opensource, cross platform and device neutral.

There are more web than objective-C developers out there.

In 2011 PhoneGap source contributed to Apache Cordovaand Adobe aquires PhoneGap name & developers.

Page 73: Angular js mobile   jsday 2014 - Verona 14 may

WHY PHONEGAP / CORDOVA?

It makes native mobile features accessible from javascript.

We can build and deploy fast.

Can be used with almost any web framework.

Page 74: Angular js mobile   jsday 2014 - Verona 14 may

WHY PHONEGAP / CORDOVA?

We can debug iOs in safari and android with chrome

Page 75: Angular js mobile   jsday 2014 - Verona 14 may

WHAT'S BAD WITH PHONEGAP / CORDOVA?

Performance

(especially for intensive graphic on old browsers)

Page 76: Angular js mobile   jsday 2014 - Verona 14 may

PHONEGAP / CORDOVA TIPS

Don't call Cordova (native) js functions until after devicereadyfires oruse .

Cordova javascript callbacks should employ safeApply(to prevent errors like $apply already in progress).

Very useful is to check out apps build with angular:

angular-phonegap-ready

(search for "mobile" tag)

Page 77: Angular js mobile   jsday 2014 - Verona 14 may

IONIC

As for Twitter Bootstrap,

Ionic is a complete framework

to build performant Apps

on top of AngularJs.

Page 78: Angular js mobile   jsday 2014 - Verona 14 may

IONIC: INSTALL

The installation is similar to cordova, it needs :

and (or ).

And then the needed npm packages:

Ant

$ sudo apt-get -u install ant

androidSDK xCode

$ npm install -g cordova ionic$ ionic start myApp tabs$ cd myApp

Page 79: Angular js mobile   jsday 2014 - Verona 14 may

IONIC: EMULATE

Emulate and build for a platform is simple like that:

$ ionic platform add android$ ionic build android$ ionic emulate android

Page 80: Angular js mobile   jsday 2014 - Verona 14 may

IONIC: LAYOUT

Ionic is full of html components.

Check out the , you will find examples of the available UIs.I really like the .

Ionic Docshtml5 input types

Page 81: Angular js mobile   jsday 2014 - Verona 14 may

IONIC: ANGULARJS

It use angular binding & controllers under the hood.

Page 82: Angular js mobile   jsday 2014 - Verona 14 may

IONIC: YEOMAN GENERATOR

There is also a Yeoman generator: generator-ionic

$ npm install -g generator-ionic$ mkdir my-ionic-project && cd $_$ yo ionic$ grunt serve

Page 83: Angular js mobile   jsday 2014 - Verona 14 may

APPGYVERFills the gap between cordova and performances.

Page 84: Angular js mobile   jsday 2014 - Verona 14 may

APPGYVER: INSTALL

Installing is similar to Cordova and Ionicyou will also need or higher,

and .

AppGyverpython 2.7

Steroids CLI

Page 85: Angular js mobile   jsday 2014 - Verona 14 may

APPGYVER: GENERATE PROJECTOnce steroids CLI is available all you have to do is:

$ steroids create myProject

$ cd myProject

Page 86: Angular js mobile   jsday 2014 - Verona 14 may

APPGYVER: EMULATE

On your device: download AppGyver Scanner for iOS / Android

On your computer launch the steroids server:

Connect your device to the same WLAN as your computer and scann the QR thatappear on the monitor.

You will be able to test your project on your device in real time.

$ steroids connect --watch

Page 87: Angular js mobile   jsday 2014 - Verona 14 may

APPGYVER: TUTORIALIf you want to discover more about AppGyver try the tutorial:

$ steroids generate tutorial begin

Page 88: Angular js mobile   jsday 2014 - Verona 14 may

APPGYVER: BUGS!

Unfortunately those are relative new technologies.

There are some for example(and that's my phone)

bugs with android 4.4

Page 89: Angular js mobile   jsday 2014 - Verona 14 may

SUMMARYThink mobile first

Make it responsive

Give Feedback

Avoid unnecessary HTTP requests

Optimize images, CSS and JS

Test!

Use AngularJs (wisely)

Use local storages to handle lose of connection

Go Hybrid using AppGyver and Ionic on top of PhoneGap

Page 90: Angular js mobile   jsday 2014 - Verona 14 may

REFERENCE / ARTICLESYeoman - Grunt - Bower

Ben Nadel: AngularJs experience

Responsive

Media Queries

Don't mess with viewport

Optimize Images

responsiveimages.org

Webp

The state of the spinner

HTTP2

Spdy

CSS animations vs JS

Use jQuery wisely

ngNewsletter - mobile

High Performance JavaScript

Even Faster Web Sites

Google Performance Tips

You have ruined Javascript.

Safari localStorage nightmare

Mobile frameworks

Page 93: Angular js mobile   jsday 2014 - Verona 14 may

REFERENCE / OTHERS(surf it with a mobile device or choose "switch to mobile" at the bottom)

Namshi.com

Google Web Fundamentals

Going mobile with angular

AngularJs and phoneGap

ngConf 2014 mobile

Page 94: Angular js mobile   jsday 2014 - Verona 14 may

THANKS!

QUESTIONS?(I hope so!)

:

:

adamquadmon

@lucianoamodio


Recommended