Slide 1

Slide 1 text

The Modern JavaScript Application Andy Appleton @appltn http://appleton.me

Slide 2

Slide 2 text

JS is growing up we are doing more and more with it and we need more robust tools

Slide 3

Slide 3 text

$(function(){ $('.thing').on('click', function(ev){ ev.preventDefault(); var $link = $(this) $.get($link.attr('href'), function(data) { $link.fadeOut(function(){ $('body').append(data); }); }); }); });

Slide 4

Slide 4 text

We can do better other languages have rich sets of tools and patterns, we can too!

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Models var Person = Backbone.Model.extend({ sayName: function(){ return this.get('name'); } }); var andy = new Person({ name: 'Andy' }); andy.sayName(); // => 'Andy'

Slide 9

Slide 9 text

Collections var People = Backbone.Collection.extend({ sayNames: function(){ return this.map(function(model){ return model.get('name'); }).join(', '); } });

Slide 10

Slide 10 text

Views var PersonView = Backbone.view.extend({ render: function(){ this.el.innerHTML = this.model.get('name'); } });

Slide 11

Slide 11 text

Tools

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Modules defined chunks of code which list their own dependencies

Slide 15

Slide 15 text

Our Model From Earlier define(['path/to/dependency'], function(dependency){ return Backbone.Model.extend({ // Adding methods and properties }); });

Slide 16

Slide 16 text

Alternative Syntax define(function(require){ var dependency = require('path/to/dep'); return Backbone.Model.extend({ // Adding methods and properties }); });

Slide 17

Slide 17 text

Structure & Conventions Split each module into a separate file

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

The Requests!

Slide 20

Slide 20 text

The Requests! The Humanity!

Slide 21

Slide 21 text

r.js The RequireJS build tool

Slide 22

Slide 22 text

$ node r.js -o config.js Concatenate & minify JS into a single file Concatenate & minify CSS via @import

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Templating

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

http://handlebarsjs.com/

Slide 27

Slide 27 text

var input = '

{{ name }}

'; var templateFn = Handlebars.compile(input); var output = templateFn({ name: 'Andy' }); // => '

Andy

'

Slide 28

Slide 28 text

var input = ' \ \

{{title}}

\ by {{author}} on {{date}}\ \ {{content}}\ \ ';

Slide 29

Slide 29 text

index.html <article> <header> <h1>{{title}}</h1> ...etc my-view.js var input = $('#tpl-name').text(); // ...compile etc

Slide 30

Slide 30 text

https://github.com/SlexAxton/require-handlebars-plugin

Slide 31

Slide 31 text

my-view.js define(function(require){ var templateFn = require('hbs!path/to/tpl'); // View now has access to compiled template // function }); post-template.hbs

{{title}}

...etc

Slide 32

Slide 32 text

my-view.js define(function(require){ var templateFn = require('hbs!path/to/tpl'); // View now has access to compiled template // function }); post-template.hbs

{{title}}

...etc

Slide 33

Slide 33 text

Handy Patterns

Slide 34

Slide 34 text

Mediator A central object to publish and subscribe to global events

Slide 35

Slide 35 text

var mediator = _.clone(Backbone.Events); mediator.on('eventname', function(args){ console.log(args); }); mediator.trigger('eventname', 'sausages'); // => 'sausages' mediator.off('eventname');

Slide 36

Slide 36 text

Model Caching sharing model instances across views

Slide 37

Slide 37 text

define(function(require){ var _cache = {}, Model = Backbone.Model.extend(); Model.create = function(opts) { if (opts.id && !_cache[opts.id]) { _cache[opts.id] = new Model(opts); } return _cache[opts.id]; }; return Model; });

Slide 38

Slide 38 text

define(function(require){ var _cache = {}, Model = Backbone.Model.extend(); Model.create = function(opts) { if (opts.id && !_cache[opts.id]) { _cache[opts.id] = new Model(opts); } return _cache[opts.id]; }; return Model; });

Slide 39

Slide 39 text

define(function(require){ var _cache = {}, Model = Backbone.Model.extend(); Model.create = function(opts) { if (opts.id && !_cache[opts.id]) { _cache[opts.id] = new Model(opts); } return _cache[opts.id]; }; return Model; });

Slide 40

Slide 40 text

Unit Testing

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

http://pivotal.github.com/jasmine/

Slide 43

Slide 43 text

var Person = Backbone.Model.extend({ sayName: function(){ return this.get('name'); } }); Our Model From Earlier (again)

Slide 44

Slide 44 text

describe('Person model', function(){ describe('#sayName', function(){ it('returns `name` attribute', function(){ var person = new Person({ name: 'Andy' }); expect(person.sayName()).toEqual('Andy'); }); }); });

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

Automation

Slide 47

Slide 47 text

Grunt JS command line build tool http://gruntjs.com/

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

+ http://phantomjs.org/

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

https://github.com/mrappleton/jasmine-examples

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

Andy Appleton @appltn http://appleton.me