title: Require js backbone JS global models
body: <p>I want to create a UserSession model, which loads and saves the session ID into a cookie using the jQuery cookie plugin.</p>
  
  <p>This is my code for my UserSession model module:</p>
  
  <pre><code>define(['jQuery', 'Underscore', 'Backbone'],
  function($, _, Backbone){
  
      var UserSession = Backbone.Model.extend({
          defaults: {
              'accessToken': null,
              'userId': null
          },
          initialize: function(){
              this.load();
          },
          authenticated: function(){
              return Boolean(this.get('accessToken'));
          },
          save: function(authHash){
              $.cookie('userId', authHash.id);
              $.cookie('accessToken', authHash.accessToken);
          },
          load: function(){
              this.userId = $.cookie('userId');
              this.accessToken = $.cookie('accessToken');
          }
      })
  
      return UserSession;
  });
  </code></pre>
  
  <p>But lets say I want to access it into my login view:</p>
  
  <pre><code>define(['jQuery', 'Underscore', 'Backbone', 'text!templates/login.html', 'models/UserLogin', 'models/UserSession'],
  function($, _, Backbone, loginTemplate, UserLogin, UserSession){
  
      var LoginView = Backbone.View.extend({
          model: new UserLogin,
          el: $('#screen'),
          events: {
              'submit #frm-login': 'login'
          },
          login: function(e){
              e.preventDefault(); // Lets not actually submit.
  
              this.model.set({
                  'username': $('#login-username').val(),
                  'password': $('#login-password').val()
              });
  
              this.model.save(null, {
                  success: function(nextModel, response){
                      // Do something here with UserSession model
                  },
                  error: function(){
                  }
              });
          },
  
          render: function(){
              $(this.el).html(_.template(loginTemplate, {}));
              return this;
          }
      });
  
      return new LoginView;
  });
  </code></pre>
  
  <p>The thing is each time I access the UserSession model in modules (see the model.save success call back function) it uses default values, so I need to have some kind of singleton instance of the UserSession model, how can I do this? </p>
  
  <p>My first idea was to use the app namespace so in our main.js (first main module that gets loaded) and there initialize the UserSession module, and each time another module access that module, the require the main module which has a object that returns the UserSession instance.</p>
  
  <p>How can this be done best?</p>
  
  <p>Thanks</p>