sf cordova meetup

46
Cordova development with AngularJS and UI component libraries

Upload: andreas-argelius

Post on 02-Jul-2015

601 views

Category:

Software


2 download

DESCRIPTION

Presentation about using AngularJS in Cordova development.

TRANSCRIPT

Page 1: SF Cordova Meetup

Cordova development with AngularJS… and UI component libraries

Page 2: SF Cordova Meetup

Me● Andreas Argelius● From Sweden● Living in Japan● Loves food and beer

Page 3: SF Cordova Meetup

Me● Works for Asial Corporation● Developer on Onsen UI

… so I might be slightly biased towards AngularJS and Onsen UI

Page 4: SF Cordova Meetup

CordovaHybrid apps using web technologies.

Java Objective-C

Page 5: SF Cordova Meetup

SPA (Single Page Application)The whole app in one HTML file

● No page reload● Manipulate DOM with JavaScript● Load content dynamically

More fluid and “native” feel.

Page 6: SF Cordova Meetup

SPA Frameworks

Page 7: SF Cordova Meetup

AngularJS● Client-side framework● Separates DOM manipulation from logic● Two-way data binding

Page 8: SF Cordova Meetup

AngularJS - featuresServicesObjects that can be inserted in various places through dependency injection.

DirectivesDefine reusable custom HTML tags, attributes or classes.

ControllersControl application state.

Page 9: SF Cordova Meetup

ServicesSubstitutable objects that can be used throughout your application.

What can we use services for? ● Data storage, e.g. database models● Calling APIs (both local and external)

Page 10: SF Cordova Meetup

Services - examplevar module = angular.module('myServices', []);

module.factory('$myService', function() {

return {

doSomething: function() {

// Do stuff!

}

};

});

Page 11: SF Cordova Meetup

Dependency injectionInject dependencies into controllers, directives and services

var app = angular.module('myApp', ['myServices']);

app.controller('MyController', function($myService) {

$myService.doSomething();

});

Page 12: SF Cordova Meetup

Services - substitutable● Services should be substitutable by a service

with same API.● Use promises even if without async calls in

service.

If you switch from localStorage to some remote storage the service will be substitutable if you used promises for the original version, even if localStorage isn’t asynchronous.

Page 13: SF Cordova Meetup

Reasons for using services● Promotes modular design● Data abstraction● Consistency

Don’t repeat yourself!

Page 14: SF Cordova Meetup

Services are singleton instances

Only initialized once (lazily)

Page 15: SF Cordova Meetup

Services in CordovaPossible usage in Cordova development:

Angularize native APIsWrap API calls in AngularJS services

Page 16: SF Cordova Meetup

Example - GeoLocationangular.module('app.services', []).

factory('$geolocation', function($q) {

return {

get: function() {

var deferred = $q.defer();

...

return deferred.promise;

}

};

});

Page 17: SF Cordova Meetup

Example - GeoLocationangular.module('app', ['app.services']).

controller('PositionController', function($scope, $geolocation) {

$geolocation.get().then(function(position) {

$scope.currentPosition = position;

},

function(error) {

// Handle error.

})

}); https://gist.github.com/argelius/2ecf0b7c08f31a11eaff

Page 18: SF Cordova Meetup

ngCordova● Camera, GeoLocation, Accelerometer, etc.● AngularJS services● Uses $q promises

github.com/driftyco/ng-cordova

Page 19: SF Cordova Meetup

Example - GeoLocationangular.module('app', ['ngCordova']).

controller('LocationController', function($cordovaGeolocation) {

$cordovaGeolocation.

getCurrentPosition().

then(function(position) {

// Do stuff.

}, ...);

});

Page 20: SF Cordova Meetup

ngTouchTouch events and helpers.

● Attribues: ng-click, ng-swipe-left, ng-swipe-right

● $swipe service, to capture touch events.

Page 21: SF Cordova Meetup

ngTouchPart of standard library

<script src=”angular-touch.js”></script><script> angular.module('app', ['ngTouch']);

</script>

Page 22: SF Cordova Meetup

ngClick<button ng-click=”doSomething()”> Click me!</button>

Replaces regular ng-click. Mobile browsers introduce a 300ms delay. ngTouch removes delay.

Page 23: SF Cordova Meetup

Swipe handlers<div ng-controller=”CarouselCtrl” ng-swipe-left=”next()”ng-swipe-right=”prev()”>...

</div>

Page 24: SF Cordova Meetup

$swipe serviceOnly one method: $swipe.bind()

$swipe.bind({

start: function(e) {

// listens for 'mousedown' or 'touchstart'

console.log('You tapped (' + e.x + ',' + e.y + ')');

}

// also 'move', 'end' and 'cancel'.

}

Page 25: SF Cordova Meetup

Directives● Attach a behavior or change a DOM element● Create custom tags, attributes or classes● Allows for cleaner markup

Using:

module.directive( 'myDirective', function($dep1, $dep2) {

...

});

Page 26: SF Cordova Meetup

Directives - exampleangular.module('app', [])

.directive('myOwnToolbar', function() {

return {

restrict: 'E'

// Lots of options that control

// the behavior of the directive.

};

});

Page 27: SF Cordova Meetup

Directives - exampleUse it in your markup:

<my-own-toolbar> My very own application! </my-own-toolbar>

Page 28: SF Cordova Meetup

Memory leaksYour directives may leak, be careful!

Be nice to the garbage collector.

Page 29: SF Cordova Meetup

Memory leaksIf an object owns a reference to the DOM element, you must remove it when the directive is removed from the DOM.

scope.$on('$destroy', function() {

this._element = null;

}.bind(this));

Also remove event listeners!

Page 30: SF Cordova Meetup

Memory leaks

● Mobile devices have less memory than desktop computers

● Hybrid apps may be open for a very long time● Even a small memory leak may be disastrous!

Page 31: SF Cordova Meetup

Chrome TimelineDetect memory leaks. Compare state before and after leaking action.

Number of nodes should be same when returning to original state.

Page 32: SF Cordova Meetup

Directives in CordovaNice way to use directives in Cordova:

● Emulate native UI components to make users feel at home.

● Utilize native APIs directly from the markup.

Page 33: SF Cordova Meetup

Native app components● Page navigation● Tab bar● Toolbar● List view

Cordova apps shouldn’t feel like web applications!

Page 34: SF Cordova Meetup

UI components

Lots of other frameworks also available:http://propertycross.com/

Page 35: SF Cordova Meetup

Built on top of Angular● Ionic and Onsen use AngularJS to create

custom tags.● Natural to write the app using AngularJS since

it’s already loaded.

Page 36: SF Cordova Meetup

Onsen UI ng-modelIntegrates with AngularJS

<ons-switch ng-model=”switchState”></ons-switch>

<p>State is: {{ switchState }}</p>

Page 37: SF Cordova Meetup

Onsen UI ng-modelWorks like a checkbox:

Page 38: SF Cordova Meetup

Components example● Sample Sushifinder app● Gets position with $geolocation service we

created before● Asks foursquare API for nearby sushi

restaurants

https://github.com/argelius/ionic-sushifinder

Page 39: SF Cordova Meetup

Sushifinder - Ionic

Page 40: SF Cordova Meetup

Ionic<ion-navbar>

...

</ion-navbar>

Page 41: SF Cordova Meetup

Ionic

<ion-list>

<ion-item>

...

</ion-item>

...

</ion-list>

Page 42: SF Cordova Meetup

Ionic

<ion-tabs>

<ion-tab

icon=”icon ion-search” ></ion-tab>

...

</ion-tabs>

Page 43: SF Cordova Meetup

Onsen UI<ons-toolbar>

...

</ons-toolbar>

Page 44: SF Cordova Meetup

Onsen UI

<ons-list>

<ons-list-item modifier= ”chevron”>

...

</ons-list-item>

</ons-list>

Page 45: SF Cordova Meetup

Onsen UI

<ons-tabbar>

<ons-tabbar-item page= ”find.html”>

</ons-tabbar-item>

...

</ons-tabbar>

Page 46: SF Cordova Meetup

Thank you for listening!