Backbone.js — Introduction to client-side JavaScript MVC

60

description

Using Backbone.js to move state to the client-side and the benefits of using a JavaScript MVC framework. Delivered at SuperMondays, Newcastle upon Tyne, on 26th September 2011.

Transcript of Backbone.js — Introduction to client-side JavaScript MVC

Page 1: Backbone.js — Introduction to client-side JavaScript MVC
Page 2: Backbone.js — Introduction to client-side JavaScript MVC

Philip Poots

@pootsbook

Ruby Developer

Audacio.us

3

18

3

Page 3: Backbone.js — Introduction to client-side JavaScript MVC
Page 4: Backbone.js — Introduction to client-side JavaScript MVC
Page 5: Backbone.js — Introduction to client-side JavaScript MVC
Page 6: Backbone.js — Introduction to client-side JavaScript MVC
Page 7: Backbone.js — Introduction to client-side JavaScript MVC
Page 8: Backbone.js — Introduction to client-side JavaScript MVC
Page 9: Backbone.js — Introduction to client-side JavaScript MVC
Page 10: Backbone.js — Introduction to client-side JavaScript MVC
Page 11: Backbone.js — Introduction to client-side JavaScript MVC
Page 12: Backbone.js — Introduction to client-side JavaScript MVC

JavaScript

Page 13: Backbone.js — Introduction to client-side JavaScript MVC

Structure

State

Speed

Page 14: Backbone.js — Introduction to client-side JavaScript MVC

Structure Framework

State Application

Speed Javascript

Page 15: Backbone.js — Introduction to client-side JavaScript MVC

Structure Framework

State Application

Speed Javascript

Page 16: Backbone.js — Introduction to client-side JavaScript MVC

$.getJSON("http://example.com/?feed=json&jsonp=?", function(data){ $('#content').html("<a href=\"" + data[0].permalink + "\">" + data[0].title + "</a>"); $('#Date').html(data[0].date); $('#Excerpt').html(data[0].excerpt); $('#Excerpt').after("<a href=\"" + data[0].permalink + "\" class=\"more\">read on &raquo;</a>"); });

Page 17: Backbone.js — Introduction to client-side JavaScript MVC

MVC Model View Controller

Pattern —1979

Architecture

—Separate domain logic & UI

Server-Side MVC

—Ruby on Rails

S T R U C T U R E

Page 18: Backbone.js — Introduction to client-side JavaScript MVC

MVC on Server S T R U C T U R E

Page 19: Backbone.js — Introduction to client-side JavaScript MVC

MVC on Client S T R U C T U R E

Page 20: Backbone.js — Introduction to client-side JavaScript MVC

Backbone Model

window.Todo = Backbone.Model.extend({ defaults: { done: false }, toggle: function() { this.save( {done: !this.get("done")}

); } });

S T R U C T U R E

Page 21: Backbone.js — Introduction to client-side JavaScript MVC

Backbone Model

window.Todo = Backbone.Model.extend({ defaults: { done: false }, toggle: function() { this.save( {done: !this.get("done")}

); } });

S T R U C T U R E

Page 22: Backbone.js — Introduction to client-side JavaScript MVC

Backbone Model

window.Todo = Backbone.Model.extend({ defaults: { done: false }, toggle: function() { this.save( {done: !this.get("done")}

); } });

S T R U C T U R E

Page 23: Backbone.js — Introduction to client-side JavaScript MVC

Backbone Collection

window.TodoList = Backbone.Collection.extend({ model: Todo, localStorage: new Store("todos"), done: function() { return this.filter(function(todo) { return todo.get('done'); }); }, remaining: function() { return this.without.apply( this, this.done()); } });

S T R U C T U R E

Page 24: Backbone.js — Introduction to client-side JavaScript MVC

Backbone Collection

window.TodoList = Backbone.Collection.extend({ model: Todo, localStorage: new Store("todos"), done: function() { return this.filter(function(todo) { return todo.get('done'); }); }, remaining: function() { return this.without.apply( this, this.done()); } });

S T R U C T U R E

Page 25: Backbone.js — Introduction to client-side JavaScript MVC

Backbone Collection

window.TodoList = Backbone.Collection.extend({ model: Todo, localStorage: new Store("todos"), done: function() { return this.filter(function(todo) { return todo.get('done'); }); }, remaining: function() { return this.without.apply( this, this.done()); } });

S T R U C T U R E

Page 26: Backbone.js — Introduction to client-side JavaScript MVC

Backbone Collection

window.TodoList = Backbone.Collection.extend({ model: Todo, localStorage: new Store("todos"), done: function() { return this.filter(function(todo) { return todo.get('done'); }); }, remaining: function() { return this.without.apply( this, this.done()); } });

S T R U C T U R E

Page 27: Backbone.js — Introduction to client-side JavaScript MVC

Backbone Collection

window.TodoList = Backbone.Collection.extend({ model: Todo, localStorage: new Store("todos"), done: function() { return this.filter(function(todo) { return todo.get('done'); }); }, remaining: function() { return this.without.apply( this, this.done()); } });

S T R U C T U R E

Page 28: Backbone.js — Introduction to client-side JavaScript MVC

Backbone Collection

window.TodoList = Backbone.Collection.extend({ model: Todo, localStorage: new Store("todos"), done: function() { return this.filter(function(todo) { return todo.get('done'); }); }, remaining: function() { return this.without.apply( this, this.done()); } });

S T R U C T U R E

Page 29: Backbone.js — Introduction to client-side JavaScript MVC

Backbone View

window.TodoView = Backbone.View.extend({ tagName: "li", template: $("#item-template").template(), events: { "change .check" : "toggleDone", "dblclick .todo-content" : "edit", "click .todo-destroy" : "destroy", "keypress .todo-input" : "updateOnEnter", "blur .todo-input" : "close" },

S T R U C T U R E

Page 30: Backbone.js — Introduction to client-side JavaScript MVC

Backbone View

window.TodoView = Backbone.View.extend({ tagName: "li", template: $("#item-template").template(), events: { "change .check" : "toggleDone", "dblclick .todo-content" : "edit", "click .todo-destroy" : "destroy", "keypress .todo-input" : "updateOnEnter", "blur .todo-input" : "close" },

S T R U C T U R E

Page 31: Backbone.js — Introduction to client-side JavaScript MVC

Backbone View

window.TodoView = Backbone.View.extend({ tagName: "li", template: $("#item-template").template(), events: { "change .check" : "toggleDone", "dblclick .todo-content" : "edit", "click .todo-destroy" : "destroy", "keypress .todo-input" : "updateOnEnter", "blur .todo-input" : "close" },

S T R U C T U R E

Page 32: Backbone.js — Introduction to client-side JavaScript MVC

JS Template

window.TodoView = Backbone.View.extend({ tagName: "li", template: $("#item-template").template(), events: { "change .check" : "toggleDone", "dblclick .todo-content" : "edit", "click .todo-destroy" : "destroy", "keypress .todo-input" : "updateOnEnter", "blur .todo-input" : "close" },

S T R U C T U R E

Page 33: Backbone.js — Introduction to client-side JavaScript MVC

Backbone View

window.TodoView = Backbone.View.extend({ tagName: "li", template: $("#item-template").template(), events: { "change .check" : "toggleDone", "dblclick .todo-content" : "edit", "click .todo-destroy" : "destroy", "keypress .todo-input" : "updateOnEnter", "blur .todo-input" : "close" },

S T R U C T U R E

Page 34: Backbone.js — Introduction to client-side JavaScript MVC

Backbone View

initialize: function() { _.bindAll(this, 'render', 'close', 'remove', 'edit'); this.model.bind('change', this.render); this.model.bind('destroy', this.remove); }, render: function() { var element = jQuery.tmpl(this.template, this.model.toJSON()); $(this.el).html(element); this.input = this.$(".todo-input"); return this; },

S T R U C T U R E

Page 35: Backbone.js — Introduction to client-side JavaScript MVC

Backbone View

initialize: function() { _.bindAll(this, 'render', 'close', 'remove', 'edit'); this.model.bind('change', this.render); this.model.bind('destroy', this.remove); }, render: function() { var element = jQuery.tmpl(this.template, this.model.toJSON()); $(this.el).html(element); this.input = this.$(".todo-input"); return this; },

S T R U C T U R E

Page 36: Backbone.js — Introduction to client-side JavaScript MVC

Backbone View

toggleDone: function() { this.model.toggle(); },

S T R U C T U R E

Page 37: Backbone.js — Introduction to client-side JavaScript MVC

Backbone Router

var Workspace = Backbone.Router.extend({ routes: { "help": "help" // #help }, help: function() { ... } });

S T R U C T U R E

Page 38: Backbone.js — Introduction to client-side JavaScript MVC

Backbone Router

var Workspace = Backbone.Router.extend({ routes: { "help": "help" // #help }, help: function() { ... } });

S T R U C T U R E

Page 39: Backbone.js — Introduction to client-side JavaScript MVC

Backbone Router

var Workspace = Backbone.Router.extend({ routes: { "help": "help" // #help }, help: function() { ... } });

S T R U C T U R E

Page 40: Backbone.js — Introduction to client-side JavaScript MVC

Clean Code S T R U C T U R E

Page 41: Backbone.js — Introduction to client-side JavaScript MVC

Structure Framework

State Application

Speed Javascript

Page 42: Backbone.js — Introduction to client-side JavaScript MVC

HTTP/1.1 “It is a…stateless protocol” —RFC 2616 (June 1999)

S TAT E

Page 43: Backbone.js — Introduction to client-side JavaScript MVC

HTTP/1.1 “It is a…stateless protocol” —RFC 2616 (June 1999)

cookies

sessions

form variables

URI parameters

S TAT E

Page 44: Backbone.js — Introduction to client-side JavaScript MVC

Server Owns State

Server

S TAT E

Client

Page 45: Backbone.js — Introduction to client-side JavaScript MVC

Request GET / HTTP/1.1 S TAT E

Server Client

Page 46: Backbone.js — Introduction to client-side JavaScript MVC

Response HTTP/1.1 200 OK S TAT E

Server Client

Page 47: Backbone.js — Introduction to client-side JavaScript MVC

AJAX asynchronicity S TAT E

Server Client

Page 48: Backbone.js — Introduction to client-side JavaScript MVC

Infrastructure S TAT E

Client

Server

Page 49: Backbone.js — Introduction to client-side JavaScript MVC

Infrastructure S TAT E

Client

Web Server

Page 50: Backbone.js — Introduction to client-side JavaScript MVC

Infrastructure S TAT E

Client

Web Server

RESTful Application

Page 51: Backbone.js — Introduction to client-side JavaScript MVC

Infrastructure S TAT E

Client

Web Server

RESTful Application

Database

Page 52: Backbone.js — Introduction to client-side JavaScript MVC

Infrastructure S TAT E

Client

Web Server

RESTful API / App.

Database

JavaScript MVC App.

Page 53: Backbone.js — Introduction to client-side JavaScript MVC

Infrastructure S TAT E

Client

Local Storage

JavaScript MVC App.

Bonus!

Page 54: Backbone.js — Introduction to client-side JavaScript MVC

Structure Framework

State Application

Speed Javascript

Page 55: Backbone.js — Introduction to client-side JavaScript MVC

JavaScript is Fast Google v8 JS engine —focus on optimizing speed

It runs in the browser

—cuts out server requests —spares server resources —instantaneous UI

S P E E D

Page 56: Backbone.js — Introduction to client-side JavaScript MVC

Data Transport JSON data only —no markup

S P E E D

Page 57: Backbone.js — Introduction to client-side JavaScript MVC

Data Transport JSON data only —no markup

S P E E D

{ "id": 1, "first_name": "Philip", "last_name": "Poots", "twitter": "@pootsbook"}

"<div id=\"user_1\">\n<dl>\n<dt>Name</dt>\n<dd>Philip Poots</dd>\n<dt>Twitter handle:</dt>\n<dd>@pootsbook</dd>\n</dl>\n</div>"

vs.

Page 58: Backbone.js — Introduction to client-side JavaScript MVC

Structure Framework

State Application

Speed Javascript

Page 59: Backbone.js — Introduction to client-side JavaScript MVC

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

Page 60: Backbone.js — Introduction to client-side JavaScript MVC

JavaScript Web Apps

@maccman