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

Moving to Backbone on Rails

mdp
July 25, 2012

Moving to Backbone on Rails

Snip.it's experience with moving our from 100% Rails to a Rails backed Backbone.JS app

mdp

July 25, 2012
Tweet

Other Decks in Technology

Transcript

  1. index.html <head> <meta charset="utf-8" /> <title>Snip.it | Collect Your Web</title>

    <link rel="stylesheet" href="/stylesheets/reset.css" /> <link rel="stylesheet" href="/stylesheets/app.css" /> <script src="/javascripts/vendor.js"></script> <script src="/javascripts/app.js"></script> <script src="/scripts/launch.js"></script> </head> <body> </body> </html> Monday, July 30, 12
  2. Love debugging JS on IE8 Easier to blame back-end problems

    on front-end Not enough semicolons in Ruby land Monday, July 30, 12
  3. JS Bloat Template manipulation on both ends Functional testing, it’s

    not awesome blah.js.erb, sweet jesus no Monday, July 30, 12
  4. Views become JSON Rendering is handled in one place The

    front-end becomes an app. Mobile could just be another app. Monday, July 30, 12
  5. And better yet, version your API, cause you’re gonna screw

    it up /api/v2/users/123 class Api::V2::UsersController < ApplicationController Monday, July 30, 12
  6. module Api module V2 class UserPresenter AVAILABLE_FIELDS = [ 'name',

    'id', 'created_at', 'description', 'posts_count', 'public_folders_count' ] def initialize(user, opts = {}) @user = user @fields = user.attributes.slice(*AVAILABLE_FIELDS) end def everyone @fields end def privileged @fields.merge({ :email => @user.email, :email_import_code => @user.email_import_code }) end Monday, July 30, 12
  7. # users_controller.rb before_filter :render_mackenzie? # application.rb class MackenzieBootstrap < StandardError;

    end; rescue_from MackenzieBootstrap, :with => :mackenzie_bootstrap def render_mackenzie? if current_user.beta? raise MackenzieBootstrap end end def mackenzie_bootstrap render 'layouts/mackenzie', :layout => false end Monday, July 30, 12
  8. Moved to OAuth 2 Access token must be included with

    each API request No more CSRF tokens DON’T ACCEPT COOKIE AUTH Monday, July 30, 12
  9. def access_token if request.headers['Authorization'] && request.headers['Authorization'].index(/OAuth/i) == 0 request.headers['Authorization'].split(' ')[1].strip

    elsif params[:access_token] params[:access_token] end end def authenticated_user if access_token token = OauthAccessToken.find_by_key(access_token) raise NotAuthorized unless token token.user else nil end end Monday, July 30, 12
  10. * Hand out OAuth tokens to old users * Use

    the preload data to help * Let the app decide how to store them Monday, July 30, 12
  11. server.use (req, res, next) -> routes = [/^\/api/] match =

    false for r in routes match = req.path.search(r) >= 0 break if match return next() unless match req.headers['host'] = endpoint end = res.end res.end = -> console.log "#{req.method}: #{req.path} - #{res.statusCode}" end.call(res) proxy.proxyRequest req, res, host: endpoint port: endpointPort server.coffee Monday, July 30, 12
  12. Things you might find useful: Javascript Error Tracking: http://errorception.com/ Backbone.JS:

    http://backbonejs.org/ Brunch: http://backbonejs.org/ Monday, July 30, 12
  13. • Ruby/Javascript • Front/Back • Money • Chairs • Desks

    (You get one) • [email protected] We’re Hiring (Shocking) Monday, July 30, 12