title: Best practice for shared objects in Backbone/Require Application
body: <p>I've been developing Backbone applications for a little while now, and am just starting to learn to use Backbone with Require.js.</p>
  <p>In my backbone app that I am refactoring, I defined a namespace like this: <code>App.model.repo</code>. This model is used over and over again in different views. I do the same thing with a few collections, for example, <code>App.collection.files</code>. These models and collections are bootstrapped in with the initial index file request.</p>
  <p>I did find <a href="https://stackoverflow.com/a/10288587/158651">this example</a>, which looks like a great way to get that bootstrapped data in. However, I am struggling with the best way to reuse/share these models and collection between views.</p>
  <p>I can think of three possible solutions. Which is best and why? Or is there another solution I am missing entirely?</p>
  <p><strong>Solution 1</strong></p>
  <p>Define these common modules and collections in the index (when they are bootstrapped in), and then pass them along to each Backbone view as an option (of <code>initialize</code>).</p>
  <pre><code>define(['jquery', 'underscore', 'backbone', 'handlebars', 'text!templates/NavBar.html'], 
      function($, _, Backbone, Handlebars, template){     
          return Backbone.View.extend({
              template: Handlebars.compile(template),
              initialize: function(options){
                  this.repoModel = options.repoModel; // common model passed in
              }
          });
      }
  );
  </code></pre>
  <p>These seems clean as far as separation, but could get funky quick, with tons of things being passed all over the place.</p>
  <p><strong>Solution 2</strong></p>
  <p>Define a <code>globals</code> module, and add commonly used models and collections to it.</p>
  <pre><code>// models/Repo.js
  define(['backbone'],
      function(Backbone){
          return Backbone.Model.extend({
              idAttribute: 'repo_id'
          });
      }
  );
  
  // globals.js (within index.php, for bootstrapping data)
  define(['underscore', 'models/Repo'], 
      function(_, RepoModel){     
          var globals = {};
          
          globals.repoModel = new Repo(&lt;?php echo json_encode($repo); ?&gt;);
          
          return globals
      }
  );
  
  define(['jquery', 'underscore', 'backbone', 'handlebars', 'text!templates/NavBar.html', 'globals'], 
      function($, _, Backbone, Handlebars, template, globals){
          var repoModel = globals.repoModel; // repoModel from globals
          
          return Backbone.View.extend({
              template: Handlebars.compile(template),
              initialize: function(options){
  
              }
          });
      }
  );
  </code></pre>
  <p>Does this solution defeat the whole point of AMD?</p>
  <p><strong>Solution 3</strong></p>
  <p>Make some models and collections return an instance, instead of a constructor (effectively making them Singletons).</p>
  <pre><code>// models/repo.js
  define(['backbone'],
      function(Backbone){
          // return instance
          return new Backbone.Model.extend({
              idAttribute: 'repo_id'
          });
      }
  );
  
  // Included in index.php for bootstrapping data
  require(['jquery', 'backbone', 'models/repo', 'routers/Application'],
      function($, Backbone, repoModel, ApplicationRouter){
          repoModel.set(&lt;?php echo json_encode($repo); ?&gt;);
  
          new ApplicationRouter({el: $('.site-container')});
          Backbone.history.start();
      }
  );
  
  define(['jquery', 'underscore', 'backbone', 'handlebars', 'text!templates/NavBar.html', 'models/repo'], 
      function($, _, Backbone, Handlebars, template, repoModel){
          // repoModel has values set by index.php
          
          return Backbone.View.extend({
              template: Handlebars.compile(template),
              initialize: function(options){
  
              }
          });
      }
  );
  </code></pre>
  <p>This I worry could get real confusing about what is a constructor and what is an instance.</p>
  <p><strong>End</strong></p>
  <p>If you read this far, you are awesome! Thanks for taking the time.</p>