Can I have multiple instances of the RequireJS module?

I will obviously miss some concept / understanding and definitely the basics of JavaScript OO!

I love using RequireJS, and now my web application is more like a structured application, rather than a whole bunch of crazy code.

I'm just trying to figure out how / if the following is possible.

I have a module that acts as a basic dataservice module called dataservice_base as follows:

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 }; }); 

As you can see, I am referring to dataservices / dataservice, which is actually the main AJAX call mechanism (not shown, but actually just the main jQuery ajax call in the wrapper).

What I'm trying to do is let this dataservice base module be "instanced" like this (inside the other module, only snippet 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/'); 

As you can see, I am trying to turn on the same dataservice_base (defined above) 3 times, but in function references, I am trying to access each instance using the named vars, i.e.:

dataservice_profile, dataservice_qualifications, dataservice_subjects

.. and, of course, I'm trying to set a unique setRoute value for each of these instances, which will be used later in the module .. while using common calls (get, puts, messages, etc.).

Obviously, I am missing a few things here ... but any help to point me on the road would be greatly appreciated!

Regards, David.

+6
source share
3 answers

I think you need to include your dependency only once and use a new one . You may need refactoring so that the common functions are in the dependent module:

 define(['dataservices/dataservice'], function (dataservice) { var dataservice_profile = new dataservice(); var dataservice_qualifications = new dataservice(); var dataservice_subjects = new dataservice(); // Set the service route(s) dataservice_profile.setRoute('/api/profile/'); dataservice_qualifications.setRoute('/api/qualification/'); dataservice_subjects.setRoute('/api/subject/'); // define needs to return something return { profile: dataservice_profile, qualifications: dataservice_qualifications, subjects: dataservice_subjects }; }); 
+6
source

Yes, brain freezing or something else .. Problems with working alone sometimes!

So, as @asgoth mentioned, quite rightly had to clear my mind and think a bit!

I ended up with the dataservice_base refactored module as follows:

 define(['dataservices/dataservice'], function (dataservice) { // Set any class/static vars // Set the instance function function dataservice_base(setRoute) { var self = this; self.route = setRoute; console.log('setting route: ' + self.route); function routeUrl(route, id) { console.log('** Setting route to: ' + route); return route + (id || "") } self.getAllEntities = function (callbacks) { return dataservice.ajaxRequest('get', routeUrl()) .done(callbacks.success) .fail(callbacks.error) } self.getEntitiesById = function (id, callbacks) { return dataservice.ajaxRequest('get', routeUrl(self.route, id)) .done(callbacks.success) .fail(callbacks.error) } self.putEntity = function (id, data, callbacks) { return dataservice.ajaxRequest('put', routeUrl(self.route, id), data) .done(callbacks.success) .fail(callbacks.error) } self.postEntity = function (data, callbacks) { return dataservice.ajaxRequest('post', routeUrl(self.route), data) .done(callbacks.success) .fail(callbacks.error) } self.deleteEntity = function (id, data, callbacks) { return dataservice.ajaxRequest('delete', routeUrl(self.route, id), data) .done(callbacks.success) .fail(callbacks.error) } } // eof instance return dataservice_base; } 

and, of course, again, as @asgoth mentioned, I need, of course, to include only one link to the dataservice_base module and specify it for my needs as follows:

 define(['dataservices/dataservice_base','viewmodels/viewmodel_profile', 'viewmodels/viewmodel_qualifications', 'viewmodels/viewmodel_subjects', 'app/common'], function (dataservice_base, viewmodel_profile, viewmodel_qualifications, viewmodel_subjects, common) { var dataservice_profile = new dataservice_base('/api/profile/'); var dataservice_qualifications = new dataservice_base('/api/qualification/'); var dataservice_subjects = new dataservice_base('/api/subject/'); // do whatever now with those instance objects... } 

SO .. now everything works!

I assume that the only thing I need to do is look at the cleaning process so that these objects are released. However, they will only be once .. but still ..

thanks again @asgoth

+3
source

Just return a function instead of an object like this

 return function(){ return { // your public interface goes here }; } 

Now you can create new instances of your plugin using new componentName() .

+1
source

All Articles