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) {
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.