Top Banner
Backbone.js Jonathan Weiss MVC in the Browser
57

Introduction to Backbone.js

Jan 15, 2015

Download

Technology

Jonathan Weiss

An introduction into Backbone.js – a lightweight MVC framework. Backbone supplies structure to JavaScript-heavy applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing application over a RESTful JSON interface.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Backbone.js

Backbone.js

Jonathan Weiss

MVC in the Browser

Page 2: Introduction to Backbone.js

Who am I?

Jonathan Weiss §  Peritor GmbH in Berlin

§  Webistrano / Capistrano

§  Ruby: SimplyStored, Happening

§  FreeBSD: Original Rubygems & Rails maintainer

§  The great fire of London

2

http://www.peritor.com http://github.com/jweiss @jweiss

Page 3: Introduction to Backbone.js

Scalarium

Amazon EC2 Cluster Management §  Auto-Config

§  Self-Healing

§  Auto-Scaling

§  One-click-deployment

www.scalarium.com

3

Page 4: Introduction to Backbone.js

4

Web App History

Page 5: Introduction to Backbone.js

Web 1.0

5

Webserver

GET

PAGE

Page 6: Introduction to Backbone.js

Web 1.0

6

Webserver

GET

GET

PAGE

PAGE

Page 7: Introduction to Backbone.js

Ajax

7

Webserver

GET

PAGE

Page 8: Introduction to Backbone.js

Ajax

8

Webserver

GET

GET

PAGE

Fragment

PAGE

Page 9: Introduction to Backbone.js

Ajax

9

Logic

GET

GET

PAGE

Fragment

PAGE Load Fragment

Page 10: Introduction to Backbone.js

Ajax

10

Models, Validation, Rendering,

....

GET

GET

PAGE

Fragment

PAGE Mess for complex

apps

Page 11: Introduction to Backbone.js

Ajax

11

Can get messy really fast

§  Loading multiple HTML fragments when updating single entity

§  Handle validation errors from the server

§  Dependency between fragments, multiple views need to change on update

§  TESTING!

Page 12: Introduction to Backbone.js

12

Page 13: Introduction to Backbone.js

13

Page 14: Introduction to Backbone.js

14

Page 15: Introduction to Backbone.js

15

Page 16: Introduction to Backbone.js

Principles D R Y

Page 17: Introduction to Backbone.js

17

Page 18: Introduction to Backbone.js

Backbone.js

18

supplies structure to JavaScript-heavy applications by providing

§  Models

§  Collections

§  Views & Templating

§  Router

Page 19: Introduction to Backbone.js

Backbone.js

19

http://documentcloud.github.com/backbone/

§  BSD license

§  Depends on

§  jQuery

§  underscore.js

§  template engine (build-in, mustache, handlebars, eco, …)

§  Lightweight – no UI widgets or desktop GUI in the browser

§  Backend agnostic by talking to RESTful JSON endpoints

Page 20: Introduction to Backbone.js

MVC

Page 21: Introduction to Backbone.js

MVC

Router

Page 22: Introduction to Backbone.js

Model

Business model

Domain logic

Reusable

Page 23: Introduction to Backbone.js

Models in Backbone

Data store

Setters & setters

Events & notifications

Persistence in & out JSON (optional)

Page 24: Introduction to Backbone.js

Collection

List of models

Events & notifications

Adjust with URL / JSON

List comprehensions: map, inject, select, …

Page 25: Introduction to Backbone.js

View

Presentation

Specific to a use-case

Different views for different perspectives on a model

Page 26: Introduction to Backbone.js

Views in Backbone

Display UI & data

Templating

Respond to user input

Respond to model change

Page 27: Introduction to Backbone.js

Controller

Conducts and connects

Inspects user requests and responds

Delegate instead of work

Page 28: Introduction to Backbone.js

Router in Backbone

Map URLs to JS actions

History state management

Instantiation of initial setup

Page 29: Introduction to Backbone.js

Workflow with Backbone.js

29

Server Model

JSON

fetch

Page 30: Introduction to Backbone.js

Workflow with Backbone.js

30

Server Model

JSON

fetch

View 2 View 1

notify

Page 31: Introduction to Backbone.js

Workflow with Backbone.js

31

Server Model

JSON

fetch

View 2 View 1

notify

render

Page 32: Introduction to Backbone.js

Workflow with Backbone.js

32

Server Model

JSON

fetch

View 2 View 1

notify

render

update

inject

Page 33: Introduction to Backbone.js

Workflow with Backbone.js

Model

View 2 View 1 update, click, input,...

Page 34: Introduction to Backbone.js

Workflow with Backbone.js

Model

View 2 View 1

update

update, click, input,...

Page 35: Introduction to Backbone.js

Workflow with Backbone.js

Model

View 2 View 1

update

update, click, input,...

notify

Page 36: Introduction to Backbone.js

Workflow with Backbone.js

36

Server Model JSON

update

View 2 View 1

update

update, click, input,...

notify

Page 37: Introduction to Backbone.js

Workflow with Backbone.js

37

Model

View 2 View 1

notify

render

update

inject

Page 38: Introduction to Backbone.js

38

Show Me The Code

Page 39: Introduction to Backbone.js

Model

39

Page 40: Introduction to Backbone.js

Collection

40

Assumes RESTful JSON backed, e.g. /servers/4646 …

Page 41: Introduction to Backbone.js

View

41

Page 42: Introduction to Backbone.js

View

42

Page 43: Introduction to Backbone.js

View

43

Page 44: Introduction to Backbone.js

Custom Events

44

Page 45: Introduction to Backbone.js

Custom Events

45

Page 46: Introduction to Backbone.js

46

Page 47: Introduction to Backbone.js

Data, Events & Views

47

Pattern

§  User or server trigger data change, never update view directly

§  Data update triggers change event

§  Views subscribe to change events and re-render

§  Router to connect URLs to actions and initialize views

Page 48: Introduction to Backbone.js

48

Give Me More

Page 49: Introduction to Backbone.js

Validations

49

Failed validation will prevent set and save

Triggers error event

Page 50: Introduction to Backbone.js

List Comprehensions

50

Page 51: Introduction to Backbone.js

List Comprehensions

51

Short form for map on an attribute

Page 52: Introduction to Backbone.js

Navigation & Back Button

52

Page 53: Introduction to Backbone.js

53

Page 54: Introduction to Backbone.js

54

Page 55: Introduction to Backbone.js

55

Page 56: Introduction to Backbone.js

56

http://documentcloud.github.com/backbone/

http://peepcode.com/products/backbone-js http://peepcode.com/products/backbone-ii

Page 57: Introduction to Backbone.js

© Peritor GmbH - Alle Rechte vorbehalten

Peritor GmbH Blücherstr. 22, Hof III Aufgang 6 10961 Berlin

Tel.: +49 (0)30 69 20 09 84 0 Fax: +49 (0)30 69 20 09 84 9

Internet: www.peritor.com E-Mail: [email protected]

Q&A