SlideShare a Scribd company logo
Intro to Backbone.js
      with Rails


     by: Tim Tyrrell
      @timtyrrell
Agenda
●   Why?
●   No really, why???
●   Intro to Backbone.js
●   Using Backbone.js with Rails
●   Testing
●   Other Libraries
●   Wrap up
●   Questions
Why should I care about Backbone.js?


          Rails is awesome!
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
●   ...
This is something that you can use
to write better software
Why not Ember.js or Spine.js?
(or the dozens of other ones)
I initially decided to learn Spine.js...
... and then I had a question.
Spine.js




(Pic taken in May 2012)
Backbone.js




(Pic taken in May 2012)
Intro to Backbone.js with Rails
Why Rails instead of Node.js or
Sinatra?




 https://speakerdeck.com/u/brennandunn/p/rails-without-views
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/
Comparing Paradigms
Backbone.js         Ruby on Rails
● Models            ● Models
● Views & Routers   ● Controllers
● Templates         ● Views
● Collections
Backbone.js Model
●   Manages data for an application
●   Not tied to markup, presentation, UI
●   Validates itself
●   Query methods (fetch, save)
Backbone.js Collection
●   Manages an ordered set of models
●   Fetches, add, removes models
●   Gives you Underscore.js goodness
●   Listens for model events
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
Backbone.js Router
● Controller that responds to URL's
  ○ Hash Fragments (#page)
  ○ Standard URL's (/page)
● Generally sets up model w/ View
Backbone.js Template
● HTML to be rendered
● Template agnostic
  ○   Eco
  ○   Handlebars.js
  ○   Mustache.js
  ○   etc.
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
rails g backbone:install
rails g backbone:scaffold task
Other Backbone Scaffolded Settings
Setup the Backbone routes
Tasks Collection
Rails Scaffolded Tasks
Where will this app appear?
Find a Spot
TasksIndex View
TasksIndex Template
TasksItem View




Item Template
Fire up the rails server
Move require_tree to the bottom
Much Better
Wire-Up the Router
Refresh the Browser
Add some records to the database
Add Tasks Template Change
Add Tasks to TasksIndex View
@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)
Add New Task to Page
Delete a Task
TasksItem View
@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)
Adding a Footer
TasksIndex to Create the View
Footer View and Template
Done with the Example Now
Testing
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
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
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
Questions?
Thanks for listening!
http://speakerrate.com/talks/11021-intro-to-backbone-js-with-rails

More Related Content

What's hot (20)

PDF
AngularJS Deep Dives (NYC GDG Apr 2013)
Nitya Narasimhan
 
PDF
The MEAN stack
Nattaya Mairittha
 
PDF
Integrating React.js Into a PHP Application
Andrew Rota
 
KEY
Tools that get you laid
Swizec Teller
 
PDF
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
Nicolás Bouhid
 
PDF
JavaScript para Graficos y Visualizacion de Datos
philogb
 
PDF
Web without framework
Nicolas Jorand
 
PDF
React.js in real world apps.
Emanuele DelBono
 
PDF
Svelte JS introduction
Mikhail Kuznetcov
 
PDF
Understanding backbonejs
Nick Lee
 
PDF
New Tools for Visualization in JavaScript - Sept. 2011
philogb
 
PDF
SproutCore and the Future of Web Apps
Mike Subelsky
 
PDF
Intro to sbt-web
Marius Soutier
 
PDF
AngularJS meets Rails
Elena Torró
 
PDF
Intro to Scala.js - Scala UG Cologne
Marius Soutier
 
PDF
Nuxt.JS Introdruction
David Ličen
 
PDF
Ruby on Rails - UNISO
Lucas Renan
 
PDF
Simpler Core Data with RubyMotion
Stefan Haflidason
 
PPTX
Jenkins api
Arie Bregman
 
PPTX
Object Oriented Programing in JavaScript
Akshay Mathur
 
AngularJS Deep Dives (NYC GDG Apr 2013)
Nitya Narasimhan
 
The MEAN stack
Nattaya Mairittha
 
Integrating React.js Into a PHP Application
Andrew Rota
 
Tools that get you laid
Swizec Teller
 
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
Nicolás Bouhid
 
JavaScript para Graficos y Visualizacion de Datos
philogb
 
Web without framework
Nicolas Jorand
 
React.js in real world apps.
Emanuele DelBono
 
Svelte JS introduction
Mikhail Kuznetcov
 
Understanding backbonejs
Nick Lee
 
New Tools for Visualization in JavaScript - Sept. 2011
philogb
 
SproutCore and the Future of Web Apps
Mike Subelsky
 
Intro to sbt-web
Marius Soutier
 
AngularJS meets Rails
Elena Torró
 
Intro to Scala.js - Scala UG Cologne
Marius Soutier
 
Nuxt.JS Introdruction
David Ličen
 
Ruby on Rails - UNISO
Lucas Renan
 
Simpler Core Data with RubyMotion
Stefan Haflidason
 
Jenkins api
Arie Bregman
 
Object Oriented Programing in JavaScript
Akshay Mathur
 

Similar to Intro to Backbone.js with Rails (20)

PDF
Backbonejs on Rails
Luis Alfredo Porras Páez
 
PDF
Developing maintainable Cordova applications
Ivano Malavolta
 
PDF
Instant download Developing Backbone js Applications Addy Osmani pdf all chapter
dinetvenitja
 
PDF
Backbone.js
Ivano Malavolta
 
PDF
Developing Backbonejs Applications Early Release Addy Osmani
littelenkali
 
PDF
Developing Backbone js Applications Addy Osmani
leitaduminy
 
PDF
[2015/2016] Backbone JS
Ivano Malavolta
 
PPT
Backbone.js
Knoldus Inc.
 
PPTX
Backbone
Sayed Ahmed
 
PDF
Backbone JS for mobile apps
Ivano Malavolta
 
PPTX
Backbonejs for beginners
Divakar Gu
 
PDF
Backbone.js slides
Ambert Ho
 
KEY
Backbonification for dummies - Arrrrug 10/1/2012
Dimitri de Putte
 
PDF
Backbone.js
Ivano Malavolta
 
PDF
Javascript Application Architecture with Backbone.JS
Min Ming Lo
 
PDF
Client Side MVC with Backbone and Rails
Tom Z Zeng
 
KEY
Wwcode2
Sarah Mei
 
PPT
Backbone js
Knoldus Inc.
 
PDF
Ember vs Backbone
Abdriy Mosin
 
PDF
Backbone
Glenn De Backer
 
Backbonejs on Rails
Luis Alfredo Porras Páez
 
Developing maintainable Cordova applications
Ivano Malavolta
 
Instant download Developing Backbone js Applications Addy Osmani pdf all chapter
dinetvenitja
 
Backbone.js
Ivano Malavolta
 
Developing Backbonejs Applications Early Release Addy Osmani
littelenkali
 
Developing Backbone js Applications Addy Osmani
leitaduminy
 
[2015/2016] Backbone JS
Ivano Malavolta
 
Backbone.js
Knoldus Inc.
 
Backbone
Sayed Ahmed
 
Backbone JS for mobile apps
Ivano Malavolta
 
Backbonejs for beginners
Divakar Gu
 
Backbone.js slides
Ambert Ho
 
Backbonification for dummies - Arrrrug 10/1/2012
Dimitri de Putte
 
Backbone.js
Ivano Malavolta
 
Javascript Application Architecture with Backbone.JS
Min Ming Lo
 
Client Side MVC with Backbone and Rails
Tom Z Zeng
 
Wwcode2
Sarah Mei
 
Backbone js
Knoldus Inc.
 
Ember vs Backbone
Abdriy Mosin
 
Backbone
Glenn De Backer
 
Ad

Recently uploaded (20)

PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
Advancing WebDriver BiDi support in WebKit
Igalia
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Advancing WebDriver BiDi support in WebKit
Igalia
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Ad

Intro to Backbone.js with Rails