Slide 1

Slide 1 text

Rails Without Views Building real-world Backbone.js applications on top of Rails

Slide 2

Slide 2 text

“When I broke ground on Projector, my first real Backbone app, I went about everything the wrong way.”

Slide 3

Slide 3 text

Backbone.View → ActionController::Base Backbone.MODEL → ACTIVERECORD::BASE Backbone.COLLECTION → ...has_many?

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

“I was naïve. I was thinking too much like a hardened Rails developer.”

Slide 6

Slide 6 text

“I was naïve. I was thinking too much like a hardened Rails developer.” Here’s how you can avoid this quicksand.

Slide 7

Slide 7 text

BACKBONE 101 FOR RAILS DEVELOPERS

Slide 8

Slide 8 text

“Views are sorta like controllers. They wrap themselves around DOM elements.”

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

“Collections are managed lists of model objects.”

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

“Models are objects wrapped around JSON.”

Slide 13

Slide 13 text

MODELS ARE RESPONSIBLE FOR... ★ Providing computed properties on top of JSON objects ★ Providing query methods ★ Validating data set on the object ★ Synchronizing itself

Slide 14

Slide 14 text

“The router connects URIs to application state.”

Slide 15

Slide 15 text

THE WAY WE TRADITIONALLY HAVE WRITTEN RAILS APPLICATIONS

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

THE WAY WE WRITE CLIENT-SIDE HEAVY APPLICATIONS

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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)

Slide 21

Slide 21 text

“Creating a single-page application doesn’t mean less code on the server, more code on the client. Not much changes on the server.”

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

FOUR CORE CONCEPTS TO KEEP YOU OUT OF THE MUD.

Slide 25

Slide 25 text

BACKBONE MODELS DON’T INHERIT FROM ACTIVERECORD.

Slide 26

Slide 26 text

“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.”

Slide 27

Slide 27 text

“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.”

Slide 28

Slide 28 text

GET /pROJECTS GET /PROJECTS/ARCHIVED

Slide 29

Slide 29 text

RETHINK THE WAY YOU PRESENT DATA RELATIONSHIPS.

Slide 30

Slide 30 text

“My REST controllers don’t always represent database resources.”

Slide 31

Slide 31 text

AVOID: GET /STORIES GET /STORIES/1/COMMENTS GET /STORIES/1/TIME_LOGS

Slide 32

Slide 32 text

“Eager load to minimize requests.” “Lazy load (make more requests) for detail views.”

Slide 33

Slide 33 text

“Eager load to minimize requests.” “Lazy load (make more requests) for detail views.”

Slide 34

Slide 34 text

EXPOSE THE CURRENT SESSION TO THE CLIENT.

Slide 35

Slide 35 text

“Store your current user in a globally accessible variable .”

Slide 36

Slide 36 text

NEVER TRUST YOUR CLIENT.

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

“Access control needs to be mirrored on the client and server.”

Slide 39

Slide 39 text

THANKS! @brennandunn projectorpm.com wearetitans.net