Slide 1

Slide 1 text

Intro to Backbone.js with Rails by: Tim Tyrrell @timtyrrell

Slide 2

Slide 2 text

Agenda ● Why? ● No really, why??? ● Intro to Backbone.js ● Using Backbone.js with Rails ● Testing ● Other Libraries ● Wrap up ● Questions

Slide 3

Slide 3 text

Why should I care about Backbone.js? Rails is awesome!

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

NO REALLY, WHY SHOULD I CARE? ● Because you are a professional ● You want to be employable in the future ● You enjoy learning new things ● ...

Slide 6

Slide 6 text

This is something that you can use to write better software

Slide 7

Slide 7 text

Why not Ember.js or Spine.js? (or the dozens of other ones)

Slide 8

Slide 8 text

I initially decided to learn Spine.js... ... and then I had a question.

Slide 9

Slide 9 text

Spine.js (Pic taken in May 2012)

Slide 10

Slide 10 text

Backbone.js (Pic taken in May 2012)

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Why Rails instead of Node.js or Sinatra? https://speakerdeck.com/u/brennandunn/p/rails-without-views

Slide 13

Slide 13 text

What is Backbone.js Backbone.js gives structure to web applications by providing: ● models with key-value binding and custom events ● collections with a rich API of enumerable functions ● views with declarative event handling ● connects it all to your existing API over a RESTful JSON interface. http://documentcloud.github.com/backbone/

Slide 14

Slide 14 text

Comparing Paradigms Backbone.js ● Models ● Views & Routers ● Templates ● Collections Ruby on Rails ● Models ● Controllers ● Views

Slide 15

Slide 15 text

Backbone.js Model ● Manages data for an application ● Not tied to markup, presentation, UI ● Validates itself ● Query methods (fetch, save)

Slide 16

Slide 16 text

Backbone.js Collection ● Manages an ordered set of models ● Fetches, add, removes models ● Gives you Underscore.js goodness ● Listens for model events

Slide 17

Slide 17 text

Backbone.js View ● Controller that responds to DOM events ● Displays data from a model w/template ● Reacts to model changes ● Reacts to DOM events ● Handle presentation logic for a part of the UI

Slide 18

Slide 18 text

Backbone.js Router ● Controller that responds to URL's ○ Hash Fragments (#page) ○ Standard URL's (/page) ● Generally sets up model w/ View

Slide 19

Slide 19 text

Backbone.js Template ● HTML to be rendered ● Template agnostic ○ Eco ○ Handlebars.js ○ Mustache.js ○ etc.

Slide 20

Slide 20 text

Getting started with Rails ● rails new todo_list -T ● cd todo_list ● echo "gem 'backbone-on-rails'" >> Gemfile ● bundle ● rails g scaffold task name:string complete: boolean ● rake db:migrate ● rm -f app/views/tasks/* ● touch app/views/tasks/index.html.erb

Slide 21

Slide 21 text

rails g backbone:install

Slide 22

Slide 22 text

rails g backbone:scaffold task

Slide 23

Slide 23 text

Other Backbone Scaffolded Settings

Slide 24

Slide 24 text

Setup the Backbone routes

Slide 25

Slide 25 text

Tasks Collection

Slide 26

Slide 26 text

Rails Scaffolded Tasks

Slide 27

Slide 27 text

Where will this app appear?

Slide 28

Slide 28 text

Find a Spot

Slide 29

Slide 29 text

TasksIndex View

Slide 30

Slide 30 text

TasksIndex Template

Slide 31

Slide 31 text

TasksItem View Item Template

Slide 32

Slide 32 text

Fire up the rails server

Slide 33

Slide 33 text

Move require_tree to the bottom

Slide 34

Slide 34 text

Much Better

Slide 35

Slide 35 text

Wire-Up the Router

Slide 36

Slide 36 text

Refresh the Browser

Slide 37

Slide 37 text

Add some records to the database

Slide 38

Slide 38 text

Add Tasks Template Change

Slide 39

Slide 39 text

Add Tasks to TasksIndex View

Slide 40

Slide 40 text

@collection.create Started POST "/tasks" for 127.0.0.1 at 2012-05-20 17:16:17 -0500 Processing by TasksController#create as JSON Parameters: {"name"=>"Walk the dog", "task"=>{"name"=>"Walk the dog"}} (0.1ms) begin transaction SQL (9.7ms) INSERT INTO "tasks" ("complete", "created_at", "name", "updated_at") VALUES (?, ?, ?, ?) [["complete", nil], ["created_at", Sun, 20 May 2012 22:16:17 UTC +00:00], ["name", "Walk the dog"], ["updated_at", Sun, 20 May 2012 22:16:17 UTC +00:00]] (1.8ms) commit transaction Completed 201 Created in 19ms (Views: 2.7ms | ActiveRecord: 11.6ms)

Slide 41

Slide 41 text

Add New Task to Page

Slide 42

Slide 42 text

Delete a Task

Slide 43

Slide 43 text

TasksItem View

Slide 44

Slide 44 text

@model.destroy() Started DELETE "/tasks/5" for 127.0.0.1 at 2012-05-20 20:58:43 -0500 Processing by TasksController#destroy as JSON Parameters: {"id"=>"5"} Task Load (0.2ms) SELECT "tasks".* FROM "tasks" WHERE "tasks"."id" = ? LIMIT 1 [["id", "5"]] (0.1ms) begin transaction SQL (0.2ms) DELETE FROM "tasks" WHERE "tasks"."id" = ? [["id", 5]] (2.9ms) commit transaction Completed 204 No Content in 5ms (ActiveRecord: 3.3ms)

Slide 45

Slide 45 text

Adding a Footer

Slide 46

Slide 46 text

TasksIndex to Create the View

Slide 47

Slide 47 text

Footer View and Template

Slide 48

Slide 48 text

Done with the Example Now

Slide 49

Slide 49 text

Testing

Slide 50

Slide 50 text

Other Libraries ● ModelBinder ○ HTML Binding ○ Recursive Binding ○ Calculated Fields ● Backbone-relational ○ one-to-one ○ one-to-many ○ many-to-one ● Backbone-validation ○ Validate model properties ○ Notify users of erros

Slide 51

Slide 51 text

Wrap Up ● A Javascript client-side MV* framework can help you write better code. ○ It forces you separate the presentation logic away from the business logic ○ It helps to make you javascript more testable ○ It allows for more responsive user experience ○ It is a tool that you will want to add to your toolbox ○ These same concepts apply to other JS frameworks ○ It is fun

Slide 52

Slide 52 text

Resources http://documentcloud.github.com/backbone/ https://github.com/meleyal/backbone-on-rails https://github.com/bradphelan/jasminerice https://github.com/netzpirat/guard-jasmine https://github.com/theironcook/Backbone.ModelBinder https://github.com/thedersen/backbone.validation https://github.com/PaulUithol/Backbone-relational https://speakerdeck.com/u/brennandunn/p/rails-without-views http://blog.carbonfive.com/2011/12/19/exploring-client-side-mvc-with- backbonejs http://addyosmani.github.com/backbone-fundamentals/ http://backbone-patterns.heroku.com/ https://speakerdeck.com/u/sarahmei/p/using-backbonejs-with-rails

Slide 53

Slide 53 text

Questions?

Slide 54

Slide 54 text

Thanks for listening! http://speakerrate.com/talks/11021-intro-to-backbone-js-with-rails