title: Can I have multiple instances of a RequireJS Module?
body: <p>I am obviously missing some concept/understanding and most definitely javascript OO basics!</p>
  
  <p>I am loving using RequireJS, and my web app now looks more like a structured app now rather than a whole heap of crazy code.</p>
  
  <p>I am just struggling to understand how/if the following is possible.</p>
  
  <p>I have a module which acts as a base dataservice module called <strong>dataservice_base</strong> as follows:</p>
  
  <pre><code>define(['dataservices/dataservice'], function (dataservice) {
  
      // Private:     Route URL
      this.route = '/api/route-not-set/';
      var setRoute = function (setRoute) {
          this.route = setRoute;
          return;
      }
  
      //  Private:    Return route with/without id 
      var routeUrl = function (route, id) {
          console.log('** Setting route to: ' + route);
          return route + (id || "")
      }
  
      //  Private:    Returns all entities for given route
      getAllEntities = function (callbacks) {
          return dataservice.ajaxRequest('get', routeUrl())
          .done(callbacks.success)
          .fail(callbacks.error)
      };
  
      getEntitiesById = function (id, callbacks) {
          return dataservice.ajaxRequest('get', routeUrl(this.route, id))
          .done(callbacks.success)
          .fail(callbacks.error)
      };
  
      putEntity = function (id, data, callbacks) {
          return dataservice.ajaxRequest('put', routeUrl(this.route, id), data)
          .done(callbacks.success)
          .fail(callbacks.error)
      };
  
      postEntity = function (data, callbacks) {
          return dataservice.ajaxRequest('post', routeUrl(this.route), data)
          .done(callbacks.success)
          .fail(callbacks.error)
      };
  
      deleteEntity = function (id, data, callbacks) {
          return dataservice.ajaxRequest('delete', routeUrl(this.route, id), data)
          .done(callbacks.success)
          .fail(callbacks.error)
      };
  
      //  Public:     Return public interface
      return {
          setRoute: setRoute,
          getAllEntities: getAllEntities,
          getEntitiesById: getEntitiesById,
          putEntity: putEntity,
          postEntity: postEntity,
          deleteEntity: deleteEntity
      };
  
  });
  </code></pre>
  
  <p>As you can see, I am referencing dataservices/dataservice, which is actually the core AJAX call mechanism (not shown, but really just basic jQuery ajax call in a wrapper).</p>
  
  <p>What I am trying to do is allow this base dataservice module to be "instanced" as follows (within another module - snippet code only):</p>
  
  <pre><code>define(['dataservices/dataservice_base', 'dataservices/dataservice_base', 'dataservices/dataservice_base'], function (dataservice_profile, dataservice_qualifications, dataservice_subjects) {
  
      //  Set the service route(s)
      dataservice_profile.setRoute('/api/profile/');
      dataservice_qualifications.setRoute('/api/qualification/');
      dataservice_subjects.setRoute('/api/subject/');
  </code></pre>
  
  <p>As you can see, I am trying to include the same <strong>dataservice_base</strong>(defined above) 3 times, but in the function references, I am trying to refer to each instance by named vars i.e:</p>
  
  <p>dataservice_profile, dataservice_qualifications, dataservice_subjects</p>
  
  <p>.. and of course I am trying be able to set a unique <strong>setRoute</strong> value for each of those instances to use further on in the module.. whilst leveraging the common calls (get,puts,posts etc).</p>
  
  <p>Obviously I am missing a few things here.. but any help to point me back on the road would be very gratefully received!!</p>
  
  <p>Kind Regards,
  David.</p>