Slide 1

Slide 1 text

unsuck your backbone

Slide 2

Slide 2 text

who am i just super quick

Slide 3

Slide 3 text

@ammeep aint he handsome?

Slide 4

Slide 4 text

we have evolved from static html

Slide 5

Slide 5 text

jQuery soup

Slide 6

Slide 6 text

we n d structure

Slide 7

Slide 7 text

then came the re olution

Slide 8

Slide 8 text

{❴ mv✯ }❵

Slide 9

Slide 9 text

view model controller

Slide 10

Slide 10 text

view model Presenter

Slide 11

Slide 11 text

view model View model

Slide 12

Slide 12 text

view model arse banana oh dear!

Slide 13

Slide 13 text

data concerns view concerns SEPARATE

Slide 14

Slide 14 text

{❴ mv✯ libraries }❵ {❴ mv✯ frameworks }❵

Slide 15

Slide 15 text

whats the difference?

Slide 16

Slide 16 text

{❴ frameworks }❵ are generally prescriptive and sometimes restrictive :

Slide 17

Slide 17 text

{❴ libraries }❵ are generally focused and specific :

Slide 18

Slide 18 text

backbone (13,619) knockout (3,532) meteor (8,144) angular (8,449) ember (6,610) batman (1,321) spine (2,336) according to frameworks libraries canJS (569) ⤻ star count

Slide 19

Slide 19 text

backbone.js is SJEJDVMPVTMZ unopinionated

Slide 20

Slide 20 text

Models Views Events Router data & associated functions

Slide 21

Slide 21 text

Models Views Events Router data & associated functions ui backed by a model

Slide 22

Slide 22 text

Models Views Events Router data & associated functions ui backed by a model bind & trigger custom events

Slide 23

Slide 23 text

Models Views Events Router data & associated functions ui backed by a model bind & trigger custom events linkable URLs

Slide 24

Slide 24 text

refactoring 1st step

Slide 25

Slide 25 text

everywhere state stored

The Grinch.

meanest grinch of all.

function sayHello(){ var name = $('.name').text(); var desc = $('.desc').text(); return'Hello I’m '+ name +'The ' + desc; } sayHello();

Slide 26

Slide 26 text

backbone.js var Animal = Backbone.Model.extend({ sayHello: function () { var name = this.get('name'); return 'Hello, I am ' + name; } }); var animal = new Animal({name:'cat'}); animal.sayHello(); refactored to

Slide 27

Slide 27 text

Backbone Views var MyView = Backbone.View.extend({ template:_.template($('#animal_template').html()), initialize: function() { this.listenTo(this.model, "change", this.render); } render: function() { $(this.el).html(this.template(this.model.toJSON())); return this } });

Slide 28

Slide 28 text

focused Backbone keeps it small encapsulated Backbone keeps it Backbone keeps it

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No Strength Instructions

Slide 31

Slide 31 text

No Weakne Instructions

Slide 32

Slide 32 text

all it gives you is a base to help you into the world of well structured javascript

Slide 33

Slide 33 text

one thousand models impossible to extend one thousand views what the hell is going on

Slide 34

Slide 34 text

backbone soup

Slide 35

Slide 35 text

wait... shouldn’t mv prevent the soup? ✯ WHAT

Slide 36

Slide 36 text

Presentation Pa ern ONLY mv stars are

Slide 37

Slide 37 text

{❴ mv✯ can’t save us now }❵

Slide 38

Slide 38 text

unsuck my backbone Do I how

Slide 39

Slide 39 text

can we build scalable backbone applications

Slide 40

Slide 40 text

how do people build desktop applications can we learn anything

Slide 41

Slide 41 text

composite applications

Slide 42

Slide 42 text

The secret to building large apps is never build large apps. Break your application into small pieces. Then, assemble those testable, bite-sized pieces into your big application ” “ Justin Meyer, author JavaScriptMVC

Slide 43

Slide 43 text

Round u Views Models into Modules Round u

Slide 44

Slide 44 text

How do you spot modules in a crowded room?

Slide 45

Slide 45 text

MODULE㽉

Slide 46

Slide 46 text

MODULE

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

Module a small piece of functionality

Slide 49

Slide 49 text

Module are decoupled from each other

Slide 50

Slide 50 text

Module ask never take

Slide 51

Slide 51 text

Module don't pollute the global scope

Slide 52

Slide 52 text

A change to this module: Doesn't impact other modules

Slide 53

Slide 53 text

Put this module on a new page: and it still works!

Slide 54

Slide 54 text

still play a part ... presentation pa ern

Slide 55

Slide 55 text

⬌ How do these modules work together?

Slide 56

Slide 56 text

self organise ⬌ ⬌ ⬌ ⬌ ⬌ ⬌ ⬌ ⬌ ⬌ ⬌ ⬌ ⬌ ⬌ they could

Slide 57

Slide 57 text

⬌ ⬌ ⬌ BUGs! I’m taking you all down with me!

Slide 58

Slide 58 text

planes don't rule the runway

Slide 59

Slide 59 text

does air traffic control no flips!

Slide 60

Slide 60 text

air traffic ⬌ control

Slide 61

Slide 61 text

Placement Eventing Start Up Lifecycle A lication

Slide 62

Slide 62 text

Placement Eventing Start Up Lifecycle A lication

Slide 63

Slide 63 text

Placement Eventing Start Up Lifecycle A lication

Slide 64

Slide 64 text

Placement Eventing Start Up Lifecycle A lication fire up the engines

Slide 65

Slide 65 text

Placement Eventing Start Up Lifecycle A lication initial global setup

Slide 66

Slide 66 text

Placement Eventing Start Up Lifecycle A lication

Slide 67

Slide 67 text

Placement Eventing Start Up Lifecycle A lication do we have permission to land? go ahead

Slide 68

Slide 68 text

Placement Eventing Start Up Lifecycle A lication publish & subscribe to events

Slide 69

Slide 69 text

Placement Eventing Start Up Lifecycle A lication

Slide 70

Slide 70 text

Placement Eventing Start Up Lifecycle A lication time to take off? Not yet!

Slide 71

Slide 71 text

Placement Eventing Start Up Lifecycle A lication when is it safe for a module to start

Slide 72

Slide 72 text

Placement Eventing Start Up Lifecycle A lication

Slide 73

Slide 73 text

Placement Eventing Start Up Lifecycle A lication which runway? that runway

Slide 74

Slide 74 text

Placement Eventing Start Up Lifecycle A lication places modules inside the application shell

Slide 75

Slide 75 text

Placement Eventing Start Up Lifecycle A lication

Slide 76

Slide 76 text

focused modules round up your features into

Slide 77

Slide 77 text

pub & sub fire and forget

Slide 78

Slide 78 text

compose yourself regain control

Slide 79

Slide 79 text

built this way are flexible enough to applications accept change

Slide 80

Slide 80 text

can we build backbone.js this way? applications

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

into two parts can be split marionette

Slide 83

Slide 83 text

to backbone.js mv✯ extensions

Slide 84

Slide 84 text

ItemView CollectionView Layout mv✯ extensions 1

Slide 85

Slide 85 text

ItemView CollectionView Layout mv✯ extensions 1

Slide 86

Slide 86 text

mv✯ extensions 1 ItemView MyView = Backbone.Marionette.ItemView.extend({ template: "#some-template" }); var view = new MyView({model: new FakeModel()}); view.render();

Slide 87

Slide 87 text

ItemView CollectionView Layout mv✯ extensions 1

Slide 88

Slide 88 text

mv✯ extensions 1 CollectionView MyItemView = Backbone.Marionette.ItemView.extend({}); MyColView = Backbone.Marionette.CollectionView.extend({ itemView: MyItemView });

Slide 89

Slide 89 text

mv✯ extensions 1 CollectionView MyItemView = Backbone.Marionette.ItemView.extend({}); MyColView = Backbone.Marionette.CollectionView.extend({ itemView: MyItemView }); var view = new MyColView({collection: stuff}); view.render();

Slide 90

Slide 90 text

ItemView CollectionView Layout mv✯ extensions 1

Slide 91

Slide 91 text

mv✯ extensions 1 Layouts AppLayout = Backbone.Marionette.Layout.extend({ template: "#layout-template", regions: { menu: "#menu", // template elements content: "#content" // template elements } });

Slide 92

Slide 92 text

mv✯ extensions 1 Layouts AppLayout = Backbone.Marionette.Layout.extend({ template: "#layout-template", regions: { menu: "#menu", // template elements content: "#content" // template elements } }); var layout = new AppLayout(); layout.render(); layout.menu.show(new MenuView()); layout.content.show(new MainContentView());

Slide 93

Slide 93 text

N more Boilerplate code

Slide 94

Slide 94 text

architecture composite event driven

Slide 95

Slide 95 text

Application Eventing Modules Composite Apps 2

Slide 96

Slide 96 text

Application Eventing Modules Composite Apps 2

Slide 97

Slide 97 text

Composite Apps 2 Eventing Backbone.Wreqr

Slide 98

Slide 98 text

Composite Apps 2 Eventing Backbone.Wreqr Event Aggregator Requests Commands

Slide 99

Slide 99 text

Composite Apps 2 Eventing Backbone.Wreqr Event Aggregator Requests Commands

Slide 100

Slide 100 text

Composite Apps 2 Eventing var vent = new Backbone.Wreqr.EventAggregator(); vent.on("foo", function(){ console.log("foo event"); }); //elsewhere in your app vent.trigger("foo");

Slide 101

Slide 101 text

Composite Apps 2 Eventing Backbone.Wreqr Event Aggregator Requests Commands

Slide 102

Slide 102 text

Composite Apps 2 Eventing var reqres = new Backbone.Wreqr.RequestResponse(); reqres.setHandler("foo", function(){ return "foo requested. this is the response"; }); //elsewhere in your app var result = reqres.request("foo"); console.log(result);

Slide 103

Slide 103 text

Composite Apps 2 Eventing Backbone.Wreqr Event Aggregator Requests Commands

Slide 104

Slide 104 text

Composite Apps 2 Eventing var commands = new Backbone.Wreqr.Commands(); commands.setHandler("foo", function(){ console.log("the foo command was executed"); }); //elsewhere in your app commands.execute("foo");

Slide 105

Slide 105 text

Composite Apps 2 Eventing Backbone.Wreqr Event Aggregator Requests Commands

Slide 106

Slide 106 text

Application Eventing Modules Composite Apps 2

Slide 107

Slide 107 text

Composite apps 2

Slide 108

Slide 108 text

StartUp Eventing Lifecycle composite apps 2 Placement

Slide 109

Slide 109 text

StartUp Eventing Lifecycle composite apps 2 Placement

Slide 110

Slide 110 text

Composite Apps 2 Start Up MyApp = new Backbone.Marionette.Application(); MyApp.start();

Slide 111

Slide 111 text

Composite Apps 2 Start Up MyApp.onInitializeBefore = function(options){ //stuff }); MyApp.onInitializeAfter = function(options){ if (Backbone.history){ Backbone.history.start(); } }); MyApp.start();

Slide 112

Slide 112 text

StartUp Eventing Lifecycle Composite Apps 2 Placement

Slide 113

Slide 113 text

Composite Apps 2 Eventing MyApp = new Backbone.Marionette.Application(); MyApp.vent.on("foo", function(){ alert("bar"); }); MyApp.vent.trigger("foo"); // => alert box "bar"

Slide 114

Slide 114 text

StartUp Eventing Lifecycle composite apps 2 Placement

Slide 115

Slide 115 text

Composite Apps 2 Lifecycle // start the app MyApp.start(); // later, start the module MyApp.FooModule.start();

Slide 116

Slide 116 text

StartUp Eventing Lifecycle composite apps 2 Placement

Slide 117

Slide 117 text

Composite Apps 2 Placement var MyApp = new Backbone.Marionette.Application(); MyApp.addRegions({ navigation : '#navigation_container', content : '#content_container', rightSidebar : '#right_sidebar_container', footer : '#footer_container' });

Slide 118

Slide 118 text

Composite Apps 2 Placement MyApp.start(); MyApp.content.show(new View()); var MyApp = new Backbone.Marionette.Application(); MyApp.addRegions({ navigation : '#navigation_container', content : '#content_container', rightSidebar : '#right_sidebar_container', footer : '#footer_container' });

Slide 119

Slide 119 text

Application Eventing Modules Composite Apps 2

Slide 120

Slide 120 text

Composite Apps 2 Modules ap lica ion hang off the p t

Slide 121

Slide 121 text

Composite Apps 2 Modules MyApp.module("MyModule", function(MyModule, MyApp, Backbone) { /// insert module functionality here });

Slide 122

Slide 122 text

Composite Apps 2 Modules MyApp.module("MyModule", function(MyModule, MyApp, Backbone){ ...define models... ImageItemView = Backbone.Marionette.ItemView.extend({ template: "#image-template" }); Library.show = function (region) { region.show(new ImageItemView({model: new MyModel()})); }; });

Slide 123

Slide 123 text

Composite Apps 2 Modules MyApp.module('ImageCatalog',function(ImageCatalog, App...){ .... module initialisation code... App.commands.addHandler('catalog:add', function(image){ allImages.add(image); }); App.reqres.addHandler('catalog:get', function(){ return allImages; }); });

Slide 124

Slide 124 text

Application Eventing Modules Composite Apps 2

Slide 125

Slide 125 text

plumbing code say goodbye to crappy

Slide 126

Slide 126 text

to your events add semantic meaning

Slide 127

Slide 127 text

composite design accept the winds of change with

Slide 128

Slide 128 text

marionette.js unsuck our backbone using

Slide 129

Slide 129 text

only scratched the Surface

Slide 130

Slide 130 text

tl;dr backbone.js is awesome

Slide 131

Slide 131 text

tl;dr but scaling it is ridiculouslyhard

Slide 132

Slide 132 text

tl;dr mv help us create structure ✯

Slide 133

Slide 133 text

tl;dr they don’t help us write scaleable JavaScript a lication

Slide 134

Slide 134 text

tl;dr we need to think beyond mv✯

Slide 135

Slide 135 text

tl;dr large JavaScript applications composed from smaller parts

Slide 136

Slide 136 text

tl;dr make your application dance marionette.js

Slide 137

Slide 137 text

That it! @ammeep amy.palamounta.in internet related activities: greenbutton.com backbonejs.org marionettejs.com lostechies.com/derickbailey addyosmani.com/blog