5. 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.
7. AngularJS
● Client-side framework
● Separates DOM manipulation from logic
● Two-way data binding
8. AngularJS - features
Services
Objects that can be inserted in various places through dependency injection.
Directives
Define reusable custom HTML tags, attributes or classes.
Controllers
Control application state.
9. Services
Substitutable 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)
10. Services - example
var module = angular.module('myServices', []);
module.factory('$myService', function() {
return {
doSomething: function() {
// Do stuff!
}
};
});
11. Dependency injection
Inject dependencies into controllers, directives
and services
var app = angular.module('myApp', ['myServices']);
app.controller('MyController', function($myService) {
$myService.doSomething();
});
12. 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.
13. Reasons for using services
● Promotes modular design
● Data abstraction
● Consistency
Don’t repeat yourself!
24. $swipe service
Only 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'.
}
25. 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) {
...
});
26. Directives - example
angular.module('app', [])
.directive('myOwnToolbar', function() {
return {
restrict: 'E'
// Lots of options that control
// the behavior of the directive.
};
});
27. Directives - example
Use it in your markup:
<my-own-toolbar>
My very own application!
</my-own-toolbar>
28. Memory leaks
Your directives may leak,
be careful!
Be nice to the garbage collector.
29. Memory leaks
If 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!
30. 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!
31. Chrome Timeline
Detect memory leaks. Compare state before and after leaking
action.
Number of nodes should be same when returning to original state.
32. Directives in Cordova
Nice way to use directives in Cordova:
● Emulate native UI components to make users
feel at home.
● Utilize native APIs directly from the markup.
33. Native app components
● Page navigation
● Tab bar
● Toolbar
● List view
Cordova apps shouldn’t feel like
web applications!
34. UI components
Lots of other frameworks also available:
http://propertycross.com/
35. 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.
36. Onsen UI ng-model
Integrates with AngularJS
<ons-switch ng-model=”switchState”></ons-switch>
<p>State is: {{ switchState }}</p>
38. 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