Slide 1

Slide 1 text

Vero Rebagliatte

Slide 2

Slide 2 text

Vero Rebagliatte @rebagliatte

Slide 3

Slide 3 text

Forecast

Slide 4

Slide 4 text

Forecast • What’s backbone.js • Why to use a library/framework • Alternatives • How to pick one (libraries vs frameworks) • Backbone classes • Backbone examples

Slide 5

Slide 5 text

What’s backbone?

Slide 6

Slide 6 text

It’s a javascript library

Slide 7

Slide 7 text

It was created by J. Ashkenas

Slide 8

Slide 8 text

It’s lightweight

Slide 9

Slide 9 text

It’s free and open source

Slide 10

Slide 10 text

It provides utilities via

Slide 11

Slide 11 text

It was extracted from

Slide 12

Slide 12 text

It’s MV*

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Why do I need a library/framework?

Slide 15

Slide 15 text

Client-side rendering

Slide 16

Slide 16 text

Event-driven approach

Slide 17

Slide 17 text

Model mirroring

Slide 18

Slide 18 text

Separation of concerns

Slide 19

Slide 19 text

History

Slide 20

Slide 20 text

‘Single API - many client apps’ pattern

Slide 21

Slide 21 text

Alternatives?

Slide 22

Slide 22 text

Alternatives • Spine • Knockout • Angular • JavaScript MVC • Capuccino • SproutCore • Batman.js • Coherent • Ember • Ext • Meteor • Agility • Google Closure • Google Web Toolkit

Slide 23

Slide 23 text

Should I pick a library or a framework?

Slide 24

Slide 24 text

Library approach

Slide 25

Slide 25 text

Framework approach

Slide 26

Slide 26 text

A library gets called by me

Slide 27

Slide 27 text

I get called by frameworks (inversion of control)

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Backbone classes

Slide 30

Slide 30 text

Routers

Slide 31

Slide 31 text

Routers map urls to js methods

Slide 32

Slide 32 text

History

Slide 33

Slide 33 text

History observes url changes and triggers callbacks

Slide 34

Slide 34 text

Views

Slide 35

Slide 35 text

Views own a DOM element

Slide 36

Slide 36 text

Views might render DOM via the Templates

Slide 37

Slide 37 text

Views might observe model events

Slide 38

Slide 38 text

Views handle user input

Slide 39

Slide 39 text

Views invoke model methods

Slide 40

Slide 40 text

Views are pretty much like Rails Controllers

Slide 41

Slide 41 text

Templates

Slide 42

Slide 42 text

Templates are interpolated HTML

Slide 43

Slide 43 text

Models

Slide 44

Slide 44 text

Models represent a single record

Slide 45

Slide 45 text

Collections

Slide 46

Slide 46 text

Collections contain multiple models

Slide 47

Slide 47 text

Collections have underscore methods available

Slide 48

Slide 48 text

Models and Collections represent the data

Slide 49

Slide 49 text

Building a twitter app

Slide 50

Slide 50 text

window.Twitter = Models: {} Collections: {} Views: {} Routers: {} initialize: -> new Twitter.Routers.Tweets() Backbone.history.start() $(document).ready -> Twitter.initialize() Initializing the app

Slide 51

Slide 51 text

class Twitter.Routers.Tweets extends Backbone.Router routes: '': 'index' 'tweets': 'index' initialize: -> @collection = new Twitter.Collections.Tweets() @collection.fetch() index: -> view = new Twitter.Views.TweetsIndex(collection: @collection) $('#container').html(view.render().el) Router

Slide 52

Slide 52 text

class Twitter.Routers.Tweets extends Backbone.Router routes: '': 'index' 'tweets': 'index' initialize: -> @collection = new Twitter.Collections.Tweets() @collection.fetch() index: -> view = new Twitter.Views.TweetsIndex(collection: @collection) $('#container').html(view.render().el) Router

Slide 53

Slide 53 text

class Twitter.Routers.Tweets extends Backbone.Router routes: '': 'index' 'tweets': 'index' initialize: -> @collection = new Twitter.Collections.Tweets() @collection.fetch() index: -> view = new Twitter.Views.TweetsIndex(collection: @collection) $('#container').html(view.render().el) Router

Slide 54

Slide 54 text

class Twitter.Collections.Tweets extends Backbone.Collection model: Twitter.Models.Tweet url: '/tweets' Collection

Slide 55

Slide 55 text

class Twitter.Models.Tweet extends Backbone.Model Model

Slide 56

Slide 56 text

class Twitter.Views.TweetsIndex extends Backbone.View template: JST['tweets/index'] events: 'submit #new_tweet': 'createTweet' initialize: -> @collection.on('add', @render, this) @collection.on('reset', @render, this) render: -> $(@el).html(@template(tweets: @collection)) this createTweet: (event) -> event.preventDefault() @collection.create name: $('#new_tweet_msg').val() View

Slide 57

Slide 57 text

class Twitter.Views.TweetsIndex extends Backbone.View template: JST['tweets/index'] events: 'submit #new_tweet': 'createTweet' initialize: -> @collection.on('add', @render, this) @collection.on('reset', @render, this) render: -> $(@el).html(@template(tweets: @collection)) this createTweet: (event) -> event.preventDefault() @collection.create name: $('#new_tweet_msg').val() View

Slide 58

Slide 58 text

class Twitter.Views.TweetsIndex extends Backbone.View template: JST['tweets/index'] events: 'submit #new_tweet': 'createTweet' initialize: -> @collection.on('add', @render, this) @collection.on('reset', @render, this) render: -> $(@el).html(@template(tweets: @collection)) this createTweet: (event) -> event.preventDefault() @collection.create name: $('#new_tweet_msg').val() View

Slide 59

Slide 59 text

class Twitter.Views.TweetsIndex extends Backbone.View template: JST['tweets/index'] events: 'submit #new_tweet': 'createTweet' initialize: -> @collection.on('add', @render, this) @collection.on('reset', @render, this) render: -> $(@el).html(@template(tweets: @collection)) this createTweet: (event) -> event.preventDefault() @collection.create name: $('#new_tweet_msg').val() View

Slide 60

Slide 60 text

My Twitter App

    <% for tweet in @.tweets.models: %>
  • <%= tweet.get('content') %>
  • <% end %>
Template

Slide 61

Slide 61 text

Recap • Instantiate router • Router: Map routes with functions, load collections on them • Collection: Associate a model and a url to touch the API • Model: Add attributes logic • View: Associate a template, pass in the collection and render the result, bind events.

Slide 62

Slide 62 text

Conclusion

Slide 63

Slide 63 text

Thanks! @rebagliatte