Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Intro to Backbone.js with Rails

Intro to Backbone.js with Rails

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

Tim Tyrrell

May 22, 2012
Tweet

More Decks by Tim Tyrrell

Other Decks in Programming

Transcript

  1. Agenda • Why? • No really, why??? • Intro to

    Backbone.js • Using Backbone.js with Rails • Testing • Other Libraries • Wrap up • Questions
  2. NO REALLY, WHY SHOULD I CARE? • Because you are

    a professional • You want to be employable in the future • You enjoy learning new things • ...
  3. 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/
  4. Comparing Paradigms Backbone.js • Models • Views & Routers •

    Templates • Collections Ruby on Rails • Models • Controllers • Views
  5. Backbone.js Model • Manages data for an application • Not

    tied to markup, presentation, UI • Validates itself • Query methods (fetch, save)
  6. Backbone.js Collection • Manages an ordered set of models •

    Fetches, add, removes models • Gives you Underscore.js goodness • Listens for model events
  7. 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
  8. Backbone.js Router • Controller that responds to URL's ◦ Hash

    Fragments (#page) ◦ Standard URL's (/page) • Generally sets up model w/ View
  9. Backbone.js Template • HTML to be rendered • Template agnostic

    ◦ Eco ◦ Handlebars.js ◦ Mustache.js ◦ etc.
  10. 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
  11. @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)
  12. @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)
  13. 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
  14. 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