SlideShare a Scribd company logo
1 of 54
Download to read offline
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!
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)
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

Tools that get you laid
Tools that get you laidTools that get you laid
Tools that get you laid
Swizec Teller
 
JavaScript para Graficos y Visualizacion de Datos
JavaScript para Graficos y Visualizacion de DatosJavaScript para Graficos y Visualizacion de Datos
JavaScript para Graficos y Visualizacion de Datos
philogb
 
New Tools for Visualization in JavaScript - Sept. 2011
New Tools for Visualization in JavaScript - Sept. 2011New Tools for Visualization in JavaScript - Sept. 2011
New Tools for Visualization in JavaScript - Sept. 2011
philogb
 

What's hot (20)

AngularJS Deep Dives (NYC GDG Apr 2013)
AngularJS Deep Dives (NYC GDG Apr 2013)AngularJS Deep Dives (NYC GDG Apr 2013)
AngularJS Deep Dives (NYC GDG Apr 2013)
 
The MEAN stack
The MEAN stack The MEAN stack
The MEAN stack
 
Integrating React.js Into a PHP Application
Integrating React.js Into a PHP ApplicationIntegrating React.js Into a PHP Application
Integrating React.js Into a PHP Application
 
Tools that get you laid
Tools that get you laidTools that get you laid
Tools that get you laid
 
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
 
JavaScript para Graficos y Visualizacion de Datos
JavaScript para Graficos y Visualizacion de DatosJavaScript para Graficos y Visualizacion de Datos
JavaScript para Graficos y Visualizacion de Datos
 
Web without framework
Web without frameworkWeb without framework
Web without framework
 
React.js in real world apps.
React.js in real world apps. React.js in real world apps.
React.js in real world apps.
 
Svelte JS introduction
Svelte JS introductionSvelte JS introduction
Svelte JS introduction
 
Understanding backbonejs
Understanding backbonejsUnderstanding backbonejs
Understanding backbonejs
 
New Tools for Visualization in JavaScript - Sept. 2011
New Tools for Visualization in JavaScript - Sept. 2011New Tools for Visualization in JavaScript - Sept. 2011
New Tools for Visualization in JavaScript - Sept. 2011
 
SproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsSproutCore and the Future of Web Apps
SproutCore and the Future of Web Apps
 
Intro to sbt-web
Intro to sbt-webIntro to sbt-web
Intro to sbt-web
 
AngularJS meets Rails
AngularJS meets RailsAngularJS meets Rails
AngularJS meets Rails
 
Intro to Scala.js - Scala UG Cologne
Intro to Scala.js - Scala UG CologneIntro to Scala.js - Scala UG Cologne
Intro to Scala.js - Scala UG Cologne
 
Nuxt.JS Introdruction
Nuxt.JS IntrodructionNuxt.JS Introdruction
Nuxt.JS Introdruction
 
Ruby on Rails - UNISO
Ruby on Rails - UNISORuby on Rails - UNISO
Ruby on Rails - UNISO
 
Simpler Core Data with RubyMotion
Simpler Core Data with RubyMotionSimpler Core Data with RubyMotion
Simpler Core Data with RubyMotion
 
Jenkins api
Jenkins apiJenkins api
Jenkins api
 
Object Oriented Programing in JavaScript
Object Oriented Programing in JavaScriptObject Oriented Programing in JavaScript
Object Oriented Programing in JavaScript
 

Similar to Intro to Backbone.js with Rails

Django mongodb -djangoday_
Django mongodb -djangoday_Django mongodb -djangoday_
Django mongodb -djangoday_
WEBdeBS
 
Intro to React - Featuring Modern JavaScript
Intro to React - Featuring Modern JavaScriptIntro to React - Featuring Modern JavaScript
Intro to React - Featuring Modern JavaScript
jasonsich
 

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

Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenery
 
Angular JS, A dive to concepts
Angular JS, A dive to conceptsAngular JS, A dive to concepts
Angular JS, A dive to concepts
 
Django Mongodb Engine
Django Mongodb EngineDjango Mongodb Engine
Django Mongodb Engine
 
Javascript ui for rest services
Javascript ui for rest servicesJavascript ui for rest services
Javascript ui for rest services
 
Django mongodb -djangoday_
Django mongodb -djangoday_Django mongodb -djangoday_
Django mongodb -djangoday_
 
Intro to AngularJs
Intro to AngularJsIntro to AngularJs
Intro to AngularJs
 
Dust.js
Dust.jsDust.js
Dust.js
 
Angular JS - Introduction
Angular JS - IntroductionAngular JS - Introduction
Angular JS - Introduction
 
Combining Angular and React Together
Combining Angular and React TogetherCombining Angular and React Together
Combining Angular and React Together
 
JavaLand 2014 - Ankor.io Presentation
JavaLand 2014 - Ankor.io PresentationJavaLand 2014 - Ankor.io Presentation
JavaLand 2014 - Ankor.io Presentation
 
Benchx: An XQuery benchmarking web application
Benchx: An XQuery benchmarking web application Benchx: An XQuery benchmarking web application
Benchx: An XQuery benchmarking web application
 
Writing HTML5 Web Apps using Backbone.js and GAE
Writing HTML5 Web Apps using Backbone.js and GAEWriting HTML5 Web Apps using Backbone.js and GAE
Writing HTML5 Web Apps using Backbone.js and GAE
 
CG_CS25010_Lecture
CG_CS25010_LectureCG_CS25010_Lecture
CG_CS25010_Lecture
 
Viking academy backbone.js
Viking academy  backbone.jsViking academy  backbone.js
Viking academy backbone.js
 
MongoDB Days Silicon Valley: Building Applications with the MEAN Stack
MongoDB Days Silicon Valley: Building Applications with the MEAN StackMongoDB Days Silicon Valley: Building Applications with the MEAN Stack
MongoDB Days Silicon Valley: Building Applications with the MEAN Stack
 
FrontEnd.pdf
FrontEnd.pdfFrontEnd.pdf
FrontEnd.pdf
 
Quick start with AngularJS
Quick start with AngularJSQuick start with AngularJS
Quick start with AngularJS
 
Comparing Angular and React JS for SPAs
Comparing Angular and React JS for SPAsComparing Angular and React JS for SPAs
Comparing Angular and React JS for SPAs
 
Intro to React - Featuring Modern JavaScript
Intro to React - Featuring Modern JavaScriptIntro to React - Featuring Modern JavaScript
Intro to React - Featuring Modern JavaScript
 
Angular or Backbone: Go Mobile!
Angular or Backbone: Go Mobile!Angular or Backbone: Go Mobile!
Angular or Backbone: Go Mobile!
 

Recently uploaded

Recently uploaded (20)

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 

Intro to Backbone.js with Rails