24
SELA DEVELOPER PRACTICE Gil Fink Single Page Applications with Backbone.js

Single page applications with backbone js

Embed Size (px)

DESCRIPTION

A session I delivered in SELA SDP14 about SPAs and Backbone.js.

Citation preview

Page 1: Single page applications with backbone js

SELA DEVELOPER PRACTICE

Gil Fink

Single Page Applications

with Backbone.js

Page 2: Single page applications with backbone js

• The Road to Single Page Apps

• What is a SPA?

• SPA Building Blocks and Architecture

• AMD and RequireJS

• MVC using Backbone.js

• A Simple SPA

Agenda

Page 3: Single page applications with backbone js

RIA Web Apps

Websites

Thin Clients

and App Stores

The Road to Single Page Apps

Native Apps

Click-Once

SPA

Embedded Objects (Flash, Silverlight, Java

Applets …)

Page 4: Single page applications with backbone js

Traditional Web Apps

HTTP requests translated into user actions

The state persisted in the server side

The server translates the user action

The server translates its response to HTML

The browser displays the HTML response

Page 5: Single page applications with backbone js

Traditional Native Apps

State is persisted in the client-side

Compiled programming language is used

The application is platform dependent

An installation may be required

Page 6: Single page applications with backbone js

What is a Single Page App (SPA)?

A web application

No need for full page submit

No full refresh

No embedded objects

Client-side routing

Page 7: Single page applications with backbone js

Why to Develop SPAs?

Feature Web App Native App Single Page App

Cross Platform V X V

Client State Management

X V V

No Installation required

V X V

Web App

Native App

SPA

Page 8: Single page applications with backbone js

SPA Building Blocks

HTML5

JavaScript Libraries

Ajax

REST

SPA

Web API

Page 9: Single page applications with backbone js

HTML5

Supported by most modern browsers

Include variety of new JavaScript APIs that can help to:

Store data locally

Save data across application shutdowns

Communicate with the server in new ways

Such as CORS and Web Sockets

Increase web performance with new APIs and new mechanisms

Page 10: Single page applications with backbone js

Ajax

Asynchronously call server endpoints

Non blocking operations

You can maintain state in the client and go to the server without refreshing the whole page

Has opened the road for richer client-side UX

Page 11: Single page applications with backbone js

JavaScript Libraries/Frameworks

Page 12: Single page applications with backbone js

REST

Stands for REpresntational State Transfer

Architecture style

Designed to work with HTTP

Using HTTP verbs (POST, GET, PUT, DELETE)

Performs Create, Read, Update and Delete operations respectively

Can also use other HTTP verbs

Can receive and send data in variety of formats

JSON, XML, HTML, XHTML, Blob and more

Page 13: Single page applications with backbone js

Web API

The server is used only as API

Each API Function is an endpoint in the server

No need for server rendering

No need for server routing

Page 14: Single page applications with backbone js

Client-Side Routing

The main building block in a SPA

All routing is done in the client-side

You use a routing library/framework

When a route change happens, the library/framework intercepts the navigation and impose your functionality

Page 15: Single page applications with backbone js

SPA Architecture

Page 16: Single page applications with backbone js

Asynchronous Module Definitions (AMD)

Define modules such that the module and its dependencies can be asynchronously loaded

Page 17: Single page applications with backbone js

RequireJS Library

A module framework in the browser

Can be downloaded from http://requirejs.org/

Browser friendly API Concepts supported in Node.js as well

Defines a structure for files layout Uses conventions to perform lookups for dependencies

Page 18: Single page applications with backbone js

Demo

RequireJS

Page 19: Single page applications with backbone js

MVC using Backbone.js

Backbone.js is a small MVW library

Enforces structure to your JavaScript code

Includes only 5 main objects:

Models

Collections

Views

Routers

Events

Page 20: Single page applications with backbone js

MVC using Backbone.js – Cont.

Extending one of Backbone.js main objects

To include built-in Backbone.js functionality

To create your own custom functionality

var myModel = Backbone.Model.extend({ defaults: { myModelID: 0, myModelName: ‘’ } }); Var myCollection = Backbone.Collection.extend({ model: myModel }); var myRouter = Backbone.Router.extend({ routes: { ‘’: home }, home: function() { // create the home view } });

Page 21: Single page applications with backbone js

Demo

A Simple SPA

Page 22: Single page applications with backbone js

Questions

Page 23: Single page applications with backbone js

Summary

• SPAs are entire web apps built upon one page and JavaScript interactions

• Very suitable for mobile development

• SPAs are the way to build your next web apps!

Page 24: Single page applications with backbone js

Thank You