69
Building Single Page Applications By Rohit Ghatol (Director of Engineering @ Synerzip) @TechNext on 21 st July 2012

Building single page applications

Embed Size (px)

DESCRIPTION

Learn about Single Page Applications. Learn about various aspects of Single Page Applications, Class Systems, AMD, MVC, MVP, MVVM, Router, MicroTemplates, HTML5, CSS3 Optimization etc All the code and video demo of this presentation can be found here - http://rohitghatol.github.com/SinglePageApplication/

Citation preview

Page 1: Building single page applications

Building Single Page Applications

By Rohit Ghatol(Director of Engineering @ Synerzip)

@TechNext on 21st July 2012

Page 2: Building single page applications

Topics

• Understanding Single Page Application• Pros and Cons• Typical Architecture• Different Aspects of Single Page Applications• What Frameworks fit where?• Development Life Cycle • Future Developments to Watch out for

Page 6: Building single page applications

What are “Single Page Applications”?

Page 7: Building single page applications
Page 8: Building single page applications

Characteristics of Single Page Application

Page 9: Building single page applications

Characteristics

• No Url Change other than #• Back Button works as expected• Book mark able Links• Richer Interaction between UI Components• Ability to go Offline• Single Page is loaded, All UI built by JavaScript• Works with Restful Web Services• More such things…..

Page 10: Building single page applications

Pros and Cons

Page 11: Building single page applications

Pros and Cons

• Faster UI• More Interactive• Can be made Offline• UI is just another Client• UI can have BI• Perfect for HTML 5

Mobile apps

• Bad for SEO• More Complex to built• Need JavaScript Skills• Diff to choose JS lib• Post Dev Optimization

learning curve is involved

Page 12: Building single page applications

Typical Architecture

Page 13: Building single page applications

Typical Architecture

Models Views

Controller

PresenterView Model

Routers

AMD

Datastore

Class

Templates

Restful Web Services

Single Page Applications

Login APIs CRUD APIs Analytics

Server

Local Storage

Web Sockets

Notification

DOM APIsDeclarative

UI

Page 14: Building single page applications

Different Aspects of SPA

Page 15: Building single page applications

JavaScript framework Category

HTML 5

MVC MVP MVVM

Micro Template

AMD

Routers & History

Data Bound Views

DOM Manipula

tion

CSS Optimizatio

nDeclarativ

e UI

Mobile Apps

Production

Packaging

Class System

Page 16: Building single page applications

Class Syste

m

Page 17: Building single page applications

Test your JavaScript Skills

function foo(){ bar = “hello”;}foo();alert(bar);

HelloAny things without an var is a global variable.

Page 18: Building single page applications

Key thing about JavaScript

JavaScript is an Object Oriented Language!

But JavaScript does not have any classes!

Page 19: Building single page applications

Defining Classes

Page 20: Building single page applications

Function Approach

function Animal(){ this.name="cat"; this.getInfo = function(){ return this.name; }}

var anim = new Animal();alert(anim.name);alert(anim.getInfo());

catcat

Page 21: Building single page applications

Prototype Approach

function Animal(){ this.name="cat";}Animal.prototype.getInfo = function(){ return this.name;}

var anim = new Animal();alert(anim.name);alert(anim.getInfo());

catcat

Page 22: Building single page applications

Closure Approach

function Animal(){ var name="cat"; this.getInfo = function(){ return name; }}

var anim = new Animal();alert(anim.name);alert(anim.getInfo());

undefinedcat

Private

Page 23: Building single page applications

The JavaScrip

t book I

like !

Page 24: Building single page applications

Class Systems

• Framework define their own Class Systems

• Own ways of– Inheritance– Abstractions

• Lets see some examples!

Page 25: Building single page applications

Backbone Model Definition

Person = Backbone.Model.extend({ initialize: function(){ alert("Welcome to this world"); }});

var person = new Person({ name: "Thomas", age: 67});

Page 26: Building single page applications

Sencha’s Class Definition

Ext.define('FirstApp.model.Place',{ extend:'Ext.data.Model', config:{ fields:['id','recordId','name','icon','vicinity'] }})

Page 28: Building single page applications

Model View Controller

View

Model

ControllerFires Event

Passes Calls to

Modifies

Page 29: Building single page applications

Model View Presenter

View

Model

PresenterFires Event

Passes Calls to

Modifies

Updates

Page 30: Building single page applications

Model View ViewModel

View

Model

View Model

Fires Event

Fires Event

Modifies

Modifies

Page 31: Building single page applications

AMD

Asynchronous Module Definition

Page 32: Building single page applications

App

View

Controller Model

Store

depends

depe

nds

depend

s

depend

s

depends

JavaScript Class Dependency

Page 33: Building single page applications

Typical HTML file

<head> <script src=“model.js” …></script> <script src=“store.js” …></script> <script src=“view.js” …></script> <script src=“controller.js” …></script> <script src=“app.js” …></script></head>

Page 34: Building single page applications

With AMD

<head> <script src=“require.js” type=“…”

data-main=“app.js”></script></head>

Page 35: Building single page applications

App

View

Controller Model

Store

depends

depe

nds

depend

s

depend

s

depends

JavaScript Class Dependency

Page 36: Building single page applications

Define Module

//Model.jsdefine(function(){ return { "name":"Todo Model" };});

Page 37: Building single page applications

App

View

Controller Model

Store

depends

depe

nds

depend

s

depend

s

depends

JavaScript Class Dependency

Page 38: Building single page applications

Define Module

//Store.jsdefine([‘Model’],function(model){ return { “create”:function(){..},

“retrieve”:function(){..}, “update”:function(){..}, “delete”:function(){..},

};});

Page 39: Building single page applications

App

View

Controller Model

Store

depends

depe

nds

depend

s

depend

s

depends

JavaScript Class Dependency

Page 40: Building single page applications

Import Module

//View.jsdefine([’jQuery’,’Model’,’Store'],

function($,model, store){store.update(model);//render $(“.view”).html(…);

return ..;}) ;

Page 41: Building single page applications

App

View

Controller Model

Store

depends

depe

nds

depend

s

depend

s

depends

JavaScript Class Dependency

Page 42: Building single page applications

Import Module

//Controller.jsdefine([’jQuery’,’View’,’Store'],

function($,view, store){view.setStore(store);

$(“#placeholder”).html(view.el()); return ..;}) ;

Page 43: Building single page applications

App

View

Controller Model

Store

depends

depe

nds

depend

s

depend

s

depends

JavaScript Class Dependency

Page 44: Building single page applications

Import Module

//app.jsrequire([‘jQuery’,’Controller’],

function($,controller){$(“#loading”).html(“”);

controller.initialize();

}) ;

Page 45: Building single page applications

History & Routers

Page 46: Building single page applications

History

Page 47: Building single page applications

RoutersRouter

Page 48: Building single page applications

Micro Template

Backbone Underscore Example

Page 49: Building single page applications

<div id="search_container"></div>

<script type="text/javascript"> SearchView = Backbone.View.extend({ initialize: function(){ this.render(); }, render: function(){ // jQuery to put in html snippet in DOM $(this.el).html(“<label>Search</label><input type=“text” id=“…….”); // Load the compiled HTML into the Backbone "el" this.el.html( template ); } }); var search_view = new SearchView({ el: $("#search_container") }); </script>

Page 50: Building single page applications

<div id="search_container"></div>

<script type="text/javascript"> SearchView = Backbone.View.extend({ initialize: function(){ this.render(); }, render: function(){ // Compile the template using underscore var template = _.template( $("#search_template").html(), {} ); // Load the compiled HTML into the Backbone "el" this.el.html( template ); } }); var search_view = new SearchView({ el: $("#search_container") }); </script>

<script type="text/template" id="search_template"> <label>Search</label> <input type="text" id="search_input" /> <input type="button" id="search_button" value="Search" /> </script>

Page 51: Building single page applications

<div id="search_container"></div>

<script type="text/javascript"> SearchView = Backbone.View.extend({ initialize: function(){ this.render(); }, render: function(){

var variables = { label: "My Search" }; // Compile the template using underscore var template = _.template( $("#search_template").html(), {variables} ); // Load the compiled HTML into the Backbone "el" this.el.html( template ); } }); var search_view = new SearchView({ el: $("#search_container") }); </script>

<script type="text/template" id="search_template"> <label><%= label%></label> <input type="text" id="search_input" /> <input type="button" id="search_button" value="Search" /> </script>

Page 52: Building single page applications

Data Bound Views

Page 53: Building single page applications

View

Store

Source

Adapter

Page 54: Building single page applications

Sencha Touch Example

ListView DataStore

Model

Proxy

Reader Writer

Source

e.g List or Grid with edit capabilitiesProvides CRUD

capabilities

DataStore can be populate from Rest, JSONP, LocalStorage

Json or XML Readers and Writers

Page 55: Building single page applications

Demo Time…..

Page 56: Building single page applications

DOM Manipulatio

n

Page 57: Building single page applications

Key Points

• jQuery is the biggest name• Zeptojs is an alternative• Xui is another alternative

Page 58: Building single page applications

HTML 5

Page 59: Building single page applications

Key Points

• HTML 5 Features• Feature Detection and fall back• Polyfills

• Helpful Sites– www.html5please.com– www.html5test.com

Page 60: Building single page applications

CSS 3 Optimizatio

n

Page 61: Building single page applications

Key Points

• Sass - http://sass-lang.com/ – Imports– Variables– Nested blocks– functions

• Compass - http://compass-style.org/

Page 62: Building single page applications

Mobile Apps

Page 63: Building single page applications

Key Points

• HTML 5 Hybrid Apps– PhoneGap– Trigger IO

Page 64: Building single page applications

Production Packaging

Page 65: Building single page applications

Key Points

• Optimizing JavaScript and CSS resources– Minimizing, Concatenation, Obfuscation

• Creation of Image Sprite• Example– RequireJS Optimizer– Sencha Touch SDK Tool– etc

Page 66: Building single page applications

Not Ready for JavaScript

• Don’t have JavaScript skills– Skills not there– Willingness is not there

• Enterprise world need proven technology• Want to reuse code client/server side• Need many of the feature discussed above in

single package

Page 67: Building single page applications

Google Web Toolkit

Pros• Most Mature Single Page Application

Framework• Code in Java, compiles to JavaScript• Commercial Widget Libraries available –

Sencha EXT, Smart GWT available• Perfect for Enterprise

Page 68: Building single page applications

Google Web Toolkit

Cons• World will move to JavaScript sooner or later• Including third party JS libraries is possible but

time consuming

Page 69: Building single page applications

Dart

• See the video of Dart on Google IO 2012• Backed by Google• Runs on both Server and Browser

• Still early!!