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

Rails Without Views

Rails Without Views

A presentation I gave at RubyNation 2012. In it, I go over a few best practices for building real-world Backbone.js projects on top of Ruby on Rails.

Brennan Dunn

March 23, 2012
Tweet

More Decks by Brennan Dunn

Other Decks in Programming

Transcript

  1. “When I broke ground on Projector, my first real Backbone

    app, I went about everything the wrong way.”
  2. “I was naïve. I was thinking too much like a

    hardened Rails developer.” Here’s how you can avoid this quicksand.
  3. VIEWS ARE RESPONSIBLE FOR... ★ Rendering, or hijacking, a DOM

    element and its children ★ Responding to user input within the view’s associated element ★ Registering bindings on collections and model instances that affect the underlying DOM element ★ Templates != Backbone Views
  4. COLLECTIONS ARE RESPONSIBLE FOR... ★ Fetching data from somewhere (a

    REST API, localStorage) ★ Map/Reduce functions to be performed on the collection ★ Emitting events related to items in the collection for listeners ★ Keeping contained items sorted
  5. MODELS ARE RESPONSIBLE FOR... ★ Providing computed properties on top

    of JSON objects ★ Providing query methods ★ Validating data set on the object ★ Synchronizing itself
  6. Responsibilities of the SERVER Responsibilities of the CLIENT ★ Route

    URI to the appropriate handler ★ Do something with user input (like a form submit) ★ Authenticate sessions ★ Access control ★ Fetch stuff from a database or API ★ Render a template ★ Send back a document to the client (usually HTML) ★ Display received documents ★ Create new requests triggered by user input ★ Basic changes to the DOM
  7. Responsibilities of the SERVER Responsibilities of the CLIENT ★ Route

    URI to the appropriate handler ★ Do something with user input (like a form submit) ★ Authenticate sessions ★ Access control ★ Fetch stuff from a database or API ★ Render a template ★ Send back a document to the client (JSON) ★ Route URI to the appropriate handler ★ Fetch JSON documents from the server ★ Render templates ★ Modify templates according to user permissions ★ Attach event handlers to the resulting DOM ★ Send JSON to the backend
  8. Responsibilities of the CLIENT ★ Route URI to the appropriate

    handler ★ Fetch JSON documents from the server ★ Render templates ★ Modify templates according to user permissions ★ Attach event handlers to the resulting DOM ★ Send JSON to the backend Responsibilities of the CLIENT ★ Display received documents ★ Create new requests triggered by user input ★ Basic changes to the DOM
  9. Responsibilities of the SERVER ★ Route URI to the appropriate

    handler ★ Do something with user input (like a form submit) ★ Authenticate sessions ★ Access control ★ Fetch stuff from a database or API ★ Render a template ★ Send back a document to the client (JSON) Responsibilities of the SERVER ★ Route URI to the appropriate handler ★ Do something with user input (like a form submit) ★ Authenticate sessions ★ Access control ★ Fetch stuff from a database or API ★ Render a template ★ Send back a document to the client (usually HTML)
  10. “Creating a single-page application doesn’t mean less code on the

    server, more code on the client. Not much changes on the server.”
  11. “You can only work with what your backend provides.” “Objects

    in your collection should be based on what your client needs, not what’s in your database.”
  12. “You can only work with what your backend provides.” “Objects

    in your collection should be based on what your client needs to present, not what’s available.”