Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7171847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:29:45+00:00 2026-05-28T15:29:45+00:00

I have a backbone application which, upon load, needs to fetch data from four

  • 0

I have a backbone application which, upon load, needs to fetch data from four different collections (Rails–>JSON back end).

That’s four hits to the server, and I’m guessing there’s a better way.

I started out by trying to pass Rails to_json() of the query results into the router initialization in the Rails view such as:

<script type="text/javascript">
  $(function() {
      window.router = new Backbonedemo.Routers.CalendarsRouter({calendars: [], tasks: <%= @tasks %>});
      Backbone.history.start();
      });
</script>

but that brought no joy.

So, what’s the correct way to run the equivalent of fetch() at startup, without having to hit JSON for each collection I want to collect?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:29:45+00:00Added an answer on May 28, 2026 at 3:29 pm

    Check out the rabl gem. It allows you to customize your json response to a much greater degree than regular to_json will allow.

    Here’s a basic way to set up a project where you need to deliver a load of JSON up front:

    First, set up your controller to pull data on page load, for examlpe localhost:3000/home would look in the home controller index:

    class HomeController < ApplicationController
      def index
        @user = current_user
        render 'user.json' # this line is not actually required most of the time when using backbone and proper ajax/json requests
      end
    end
    

    Next, set up a rabl template, this takes the place of a view or a partial, and returns JSON to your client. I’m actually going to use a partial, to make loading into the home/index.html view nice and easy:

    # views/home/_user.json.rabl
    object @user
    attributes :id, :first_name, :last_name, :birthdate, :gender, :nickname, :email
    node(:avatar_thumb_url) { |u| u.avatar.url :thumb }
    node(:roles) { |u| u.roles }
    node(:name) { |u| "#{u.first_name} #{u.last_name}".strip }
    node(:errors) { |u| u.errors.to_hash if u.errors.present? }
    child :awesome_calendars => :calendars do
      attributes :id, :date, :description
      child :events do
        attributes :title, :description
      end
    end
    

    That’s some relatively fancy rabl that will deliver a bunch of json, including a related set of records, all in one JSON object.

    In your html view that loads up backbone, you need to pass the controller’s object to the partial:

    # views/home/index.html.erb
    <script type='text/javascript'>
        $(function() {
          window.router = new Backbonedemo.Routers.CalendarsRouter(<%= render('user.json.rabl', user: @user).html_safe -%>);
        Backbone.history.start();
      });
    </script>
    

    To recap:

    1. controller renders regular html.erb view (the one that starts up backbone)
    2. that view also renders a partial–this partial is a rabl template that returns strictly JSON
    3. backbone takes that JSON and does whatever you want with it.

    The beauty of this is that you can set up json.rabl responses for any of your controller actions and have them return a variety of json stuff, all easily controllable. The thing I did above is the “difficult” part where you want to load up stuff from many tables into a single JSON call on your first page load–avoiding multiple AJAX/backbone fetch requests.

    Make sense? I hope so… : / let me know if anything is unclear.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails application with the backbone-rails gem. Which works out fine but
I am trying to implement a real-time application which involves IPC across different modules.
I have a single page jquery mobile application in which there are few parent
I use Backbone.js and jQuery 1.7 in my application and I have some problems
(sorry for the english) I have a ASP .net webservice that get data from
I have a collection of models in my Backbone.js application. It's a list of
I'm planning on writing a spine/backbone.js style web application which basically just transfers a
I'm building small one page application with rails 3.1 mongodb and backbonejs. I have
Is there a way to have a different Backbone.sync method invoked for a subset
I have a Backbone view named Foo which has a scrollbar. When the scrollbar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.