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/

29773d7e4b6982e7b3c1afce2d221e07?s=128

Leonard Garvey

May 14, 2013
Tweet

Transcript

  1. @lgarvey - bottledup.net love letters from a Rails dev... Dear

    Backbone
  2. @lgarvey - bottledup.net specifically your lack of them.. I love

    your opinions
  3. @lgarvey - bottledup.net “Ember.js incorporates common idioms so you can

    focus on what makes your app special, not reinventing the wheel.” Unlike Ember
  4. @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
  5. @lgarvey - bottledup.net rails_problems.length #=> 99 rails_problems.include?(:lack_of_opinions) #=> false

  6. @lgarvey - bottledup.net I am not a JavaScript developer... Disclaimer:

  7. @lgarvey - bottledup.net

  8. @lgarvey - bottledup.net Feature: AJAX commenting In order to have

    a nice commenting experience As a troll I want to submit comments via AJAX
  9. @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()
  10. @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
  11. @lgarvey - bottledup.net if xhr? render @comment, :partial => true

    else redirect_to post end
  12. @lgarvey - bottledup.net respond_with(@comment) do |format| format.html { redirect_to post

    } format.xhr end
  13. @lgarvey - bottledup.net Rails Responders

  14. @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
  15. @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
  16. @lgarvey - bottledup.net ?