Intro to Backbone.js with Rails

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

description

Ruby on Rails is awesome…why should you care about Backbone.js? Tim will give an overview of why Rails developers should care about Javascript MVC frameworks and how you can easily integrate one into a Rails application. He’ll also will explain the structure of a Backbone.js application and how the paradigm jives with its Rails counterparts. You should be able to walk away from this presentation and add Backbone.js to your resume. Code here: https://github.com/timtyrrell/todo

Transcript of Intro to Backbone.js with Rails

Page 1: Intro to Backbone.js with Rails

Intro to Backbone.js with Rails

by: Tim Tyrrell@timtyrrell

Page 2: Intro to Backbone.js with Rails

Agenda

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

Page 3: Intro to Backbone.js with Rails

Why should I care about Backbone.js?

Rails is awesome!

Page 4: Intro to Backbone.js with Rails
Page 5: Intro to Backbone.js with Rails

NO REALLY, WHY SHOULD I CARE?

● Because you are a professional● You want to be employable in the future● You enjoy learning new things● ...

Page 6: Intro to Backbone.js with Rails

This is something that you can use to write better software

Page 7: Intro to Backbone.js with Rails

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

Page 8: Intro to Backbone.js with Rails

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

Page 9: Intro to Backbone.js with Rails

Spine.js

(Pic taken in May 2012)

Page 10: Intro to Backbone.js with Rails

Backbone.js

(Pic taken in May 2012)

Page 11: Intro to Backbone.js with Rails
Page 12: Intro to Backbone.js with Rails

Why Rails instead of Node.js or Sinatra?

https://speakerdeck.com/u/brennandunn/p/rails-without-views

Page 13: Intro to Backbone.js with Rails

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/

Page 14: Intro to Backbone.js with Rails

Comparing Paradigms

Backbone.js● Models● Views & Routers● Templates● Collections

Ruby on Rails● Models● Controllers● Views

Page 15: Intro to Backbone.js with Rails

Backbone.js Model

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

Page 16: Intro to Backbone.js with Rails

Backbone.js Collection

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

Page 17: Intro to Backbone.js with Rails

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

Page 18: Intro to Backbone.js with Rails

Backbone.js Router

● Controller that responds to URL's○ Hash Fragments (#page)○ Standard URL's (/page)

● Generally sets up model w/ View

Page 19: Intro to Backbone.js with Rails

Backbone.js Template

● HTML to be rendered● Template agnostic

○ Eco○ Handlebars.js○ Mustache.js○ etc.

Page 20: Intro to Backbone.js with Rails

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

Page 21: Intro to Backbone.js with Rails

rails g backbone:install

Page 22: Intro to Backbone.js with Rails

rails g backbone:scaffold task

Page 23: Intro to Backbone.js with Rails

Other Backbone Scaffolded Settings

Page 24: Intro to Backbone.js with Rails

Setup the Backbone routes

Page 25: Intro to Backbone.js with Rails

Tasks Collection

Page 26: Intro to Backbone.js with Rails

Rails Scaffolded Tasks

Page 27: Intro to Backbone.js with Rails

Where will this app appear?

Page 28: Intro to Backbone.js with Rails

Find a Spot

Page 29: Intro to Backbone.js with Rails

TasksIndex View

Page 30: Intro to Backbone.js with Rails

TasksIndex Template

Page 31: Intro to Backbone.js with Rails

TasksItem View

Item Template

Page 32: Intro to Backbone.js with Rails

Fire up the rails server

Page 33: Intro to Backbone.js with Rails

Move require_tree to the bottom

Page 34: Intro to Backbone.js with Rails

Much Better

Page 35: Intro to Backbone.js with Rails

Wire-Up the Router

Page 36: Intro to Backbone.js with Rails

Refresh the Browser

Page 37: Intro to Backbone.js with Rails

Add some records to the database

Page 38: Intro to Backbone.js with Rails

Add Tasks Template Change

Page 39: Intro to Backbone.js with Rails

Add Tasks to TasksIndex View

Page 40: Intro to Backbone.js with Rails

@collection.create name: @$('#add-task').val() @collection.create name: @$('#add-task').val() @collection.create name: @$('#add-task').val() @collection.create name: @$('#add-task').val() @collection.create name: @$('#add-task').val() @collection.create

Started POST "/tasks" for 127.0.0.1 at 2012-05-20 17:16:17 -0500Processing 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 transactionCompleted 201 Created in 19ms (Views: 2.7ms | ActiveRecord: 11.6ms)

Page 41: Intro to Backbone.js with Rails

Add New Task to Page

Page 42: Intro to Backbone.js with Rails

Delete a Task

Page 43: Intro to Backbone.js with Rails

TasksItem View

Page 44: Intro to Backbone.js with Rails

@model.destroy()Started DELETE "/tasks/5" for 127.0.0.1 at 2012-05-20 20:58:43 -0500Processing 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 transactionCompleted 204 No Content in 5ms (ActiveRecord: 3.3ms)

Page 45: Intro to Backbone.js with Rails

Adding a Footer

Page 46: Intro to Backbone.js with Rails

TasksIndex to Create the View

Page 47: Intro to Backbone.js with Rails

Footer View and Template

Page 48: Intro to Backbone.js with Rails

Done with the Example Now

Page 49: Intro to Backbone.js with Rails

Testing

Page 50: Intro to Backbone.js with Rails

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

Page 51: Intro to Backbone.js with Rails

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

Page 52: Intro to Backbone.js with Rails

Resourceshttp://documentcloud.github.com/backbone/https://github.com/meleyal/backbone-on-railshttps://github.com/bradphelan/jasminericehttps://github.com/netzpirat/guard-jasminehttps://github.com/theironcook/Backbone.ModelBinderhttps://github.com/thedersen/backbone.validationhttps://github.com/PaulUithol/Backbone-relational https://speakerdeck.com/u/brennandunn/p/rails-without-viewshttp://blog.carbonfive.com/2011/12/19/exploring-client-side-mvc-with-backbonejshttp://addyosmani.github.com/backbone-fundamentals/http://backbone-patterns.heroku.com/https://speakerdeck.com/u/sarahmei/p/using-backbonejs-with-rails

Page 53: Intro to Backbone.js with Rails

Questions?

Page 54: Intro to Backbone.js with Rails

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