title: Global variables for a single web page application using Durandal
body: <p>My application has three things that are "global": a user location with lat and lng values, if the user is logged in or not, and the activity that they have chosen (think of it like a category).</p>
  
  <p>What I'd like to happen is for these values to be used across the application. Each screen will use some set of this date, so loading it per page isn't the right answer. The information also will not change unless the user creates an event: different location, activity, or signs in/out.</p>
  
  <p>How should I set this up?</p>
  
  <p>My thinking is that there should be an initial load upon startup, in my main.js file I load each and then app.start like so:</p>
  
  <p><strong>siteService.js</strong></p>
  
  <pre><code>define([...], function (...) {
      return {
          init: function () {
              return $.when(
                  activityService.getStoredActivity(),
                  locationService.getStoredLocation(),
                  userService.getUsername()
              );
          }
      }
  });
  </code></pre>
  
  <p><strong>main.js</strong></p>
  
  <p>define([..., 'modules/siteService'], function (..., site) {
      ...</p>
  
  <pre><code>site.init().then(function () {
      app.start().then(function () {
          ...
          app.setRoot('viewmodels/shell', 'entrance');
      });
  });
  </code></pre>
  
  <p>});</p>
  
  <p>This does make sure that the data is loaded. From there in each of my services I store the information into local storage so I don't have to keep firing off AJAX calls. If local storage isn't there it will. </p>
  
  <p>The problem comes later when I need this data in other pages. I'm ending up calling my site.init().then() within each page's activate method. Which is pretty sloppy. </p>
  
  <p><strong>shell.js</strong></p>
  
  <pre><code>self.activate = function () {
              ...
      siteService.init().then(function (activity, location, username) {
          self.activity(activity);
          self.location(location);
          setUsername(username);
      });
      return router.activate();
  };
  </code></pre>
  
  <p>and then again in:</p>
  
  <p><strong>welcome.js</strong>      </p>
  
  <pre><code>this.activate = function () {    
       site.init().then(function(activity, location) {
             loc = location;
             act = activity;
             load();
         });
  });
  </code></pre>
  
  <p>I'd like to set the values initially, each page initially loading from those values, and then use the pub/sub of Durandal to react to any changes after the fact.</p>
  
  <p>I was thinking of solely using the pub/sub, but then felt that I would run into chicken and the egg issues when the data is loaded in relation to the page loads (IE If my AJAX loaded first, and my page wasn't loaded, the event fires, but the page never gets the update).</p>
  
  <p>Is it possible to setup something like a static class in javascript to pull the data initially and then share it throughout the viewmodels? Or, is there a way to guarantee that the value will be there when I initially load a page, like a app.on('value', { initialize: true })?</p>