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 7874009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:40:58+00:00 2026-06-03T02:40:58+00:00

Backbone.js documentation suggest loading bootstrapped models this way: <script> var Accounts = new Backbone.Collection;

  • 0

Backbone.js documentation suggest loading bootstrapped models this way:

<script>
var Accounts = new Backbone.Collection;
Accounts.reset(<%= @accounts.to_json %>);
var Projects = new Backbone.Collection;
Projects.reset(<%= @projects.to_json(:collaborators => true) %>);
</script>

But this is a pattern that can’t be used in AMD approach (using require.js)

The only possible solution is to declare global variable storing JSON data and use this variable later in relevant initialize methods.

Is there a better way to do this (without globals)?

  • 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-06-03T02:41:01+00:00Added an answer on June 3, 2026 at 2:41 am

    This is how we bootstrap data in a way that it doesn’t pollute the global namespace. Instead it uses require.js exclusively. It also helps you provide the initial app configuration based on variables within the template.

    Within your rendered page

    <script src="require.js"></script>
    <script>
    define('config', function() {
      return {
        bootstrappedAccounts: <%= @accounts.to_json %>,
        bootstrappedProjects: <%= @projects.to_json(:collaborators => true) %>
      };
    });
    </script>
    <script src="app.js"></script>
    

    globals.js

    This file checks for config and extends itself using any of the data returned

    define([
      'config',
      'underscore'
    ], function(config) {
    
      var globals = {
      };
      _.extend(globals, config);
      return globals;
    
    });
    

    config.js

    This file is needed if you want be able to load the app regardless of if you have defined config in the page.

    define(function() {
      // empty array for cases where `config` is not defined in-page
      return {};
    });
    

    app.js

    require([
      'globals',
      'underscore',
      'backbone'
    ], function(globals) {
    
      if (globals.bootstrappedAccounts) {
        var accounts = new Backbone.Collection(globals.bootstrappedAccounts);
      }
      if (globals.bootstrappedProjects) {
        var projects = new Backbone.Collection(globals.bootstrappedProjects);
      }
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following the documentation , I did: var collection = new Backbone.Collection.extend({ model: ItemModel, url:
I've seen backbone views (or models, collections etc), declared like this var SomeView =
I read in the backbone documentation that calling collection.reset() clears the collection. I want
As suggested by Backbone documentation, I provide initial data for my collection when the
My Backbone Collection receives 30 models on fetch(). I have tried newColl=origColl.first(2); to return
I'm new to Backbone.js. I have gone through the documentation. My question is where
In my backbone.js app, there is a Trips collection that holds Trip models ,
var homeView = Backbone.View.extend({ el: $(#main_container), initialize: function(){ _.bindAll(this, 'render'); }, render:function(){ $.get('/home', {},
My backbone collection fetches its models without issue. The model returns {{=calldate}} in the
In the Backbone.js documentation, in the entry for the Router.routes method , it is

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.