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

Dear Backbone. Love letters from a Rails dev.

Dear Backbone. Love letters from a Rails dev.

How I use Backbone in a traditional Rails app, while keeping as much responsibility for the application inside Rails.

See my blog post on this: http://bottledup.net/2013/05/16/dear-backbone-love-letters-from-a-rails-dev/

Leonard Garvey

May 14, 2013
Tweet

More Decks by Leonard Garvey

Other Decks in Programming

Transcript

  1. @lgarvey - bottledup.net “Ember.js incorporates common idioms so you can

    focus on what makes your app special, not reinventing the wheel.” Unlike Ember
  2. @lgarvey - bottledup.net “Angular tries to be an end-to-end solution,

    when building a web application. This means it is not a single piece in an overall puzzle of building a web application, but an end-to-end solution. This makes Angular opinionated about how a CRUD application should be built.” or Angular
  3. @lgarvey - bottledup.net Feature: AJAX commenting In order to have

    a nice commenting experience As a troll I want to submit comments via AJAX
  4. @lgarvey - bottledup.net QuickBlog.Views.Comments ||= {} class QuickBlog.Views.Comments.CreateCommentView extends Backbone.View

    events: "submit": "commentSubmitted" commentSubmitted: (e) => e.preventDefault() @submitForm() url: -> @$el.attr('action') data: -> @$el.serialize() submitForm: -> $.post(@url(), @data(), @commentPosted) commentPosted: (data) => @appendComment(data) @resetForm()
  5. @lgarvey - bottledup.net “If you'd like to create a view

    that references an element already in the DOM, pass in the element as an option” new View({el: $(‘#some_id’)}) RTFM
  6. @lgarvey - bottledup.net class CommentsController < ApplicationController respond_to :html, :xhr

    def create post = Post.find(params[:post_id]) @comment = post.comments.create!(params[:comment]) respond_with(@comment) do |format| format.html { redirect_to post } end end private def self.responder XhrResponder end end
  7. @lgarvey - bottledup.net class XhrResponder < ActionController::Responder def xhr? @controller.request.xhr?

    end # xhr the "format" when you request html but do so via AJAX. # def respond if xhr? && format == :html method = "to_xhr" else method = "to_#{format}" end respond_to?(method) ? send(method) : to_format end # render the appropriate partial for the resource # def to_xhr controller.default_render(@resource, options.merge(:partial => true)) end end