I guess instead of exporting the constructor you could export a function which checks if an instances has been created and if not create it.
If you don't want to write it as a function call, you could try set a getter on the exports and just execute the same logic in the getter. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty Documentation from MDN in case you are not familiar with property getters/setters in JS...they are not what you'd be used to from Java. On Thu, Nov 21, 2013 at 5:03 PM, Reza Razavipour <[email protected]> wrote: > Ok so this is the way i have it constructed... SoapClient is my module and I > want it to be used as a singleton, i.e. if it is initialized, it just > returns the connection, or SoapClient.instance... > > var SoapClient = function() { >> >> var soap = require('soap'); >> this.init = function (){ >> var url = "http://172.31.19.39/MgmtServer.wsdl"; >> var endPoint = "https://172.31.19.39:9088"; >> var options = {}; >> options.endpoint = endPoint; >> soap.createClient(url, options, function(err, result) { >> if (err) { >> console.log('soap client create failed ' + err); >> return; >> } >> console.log('init is called ready'); >> SoapClient.instance = result; >> SoapClient.instance.setSecurity(new soap.BasicAuthSecurity( >> 'admin-priv', 'password')); >> }); >> }; >> }; >> SoapClient.getInstance = function () { >> if (SoapClient.instance) { >> return SoapClient.instance; >> } >> new SoapClient().init(); >> return SoapClient.instance; >> }; >> SoapClient.instance = null; >> module.exports = SoapClient; > > > Thoughts > > > On Thursday, November 21, 2013 8:34:14 AM UTC-8, Reza Razavipour wrote: >> >> this I get the jist of the conversation but my Javascript skills, less >> than 2 months, does not allow me to be able to code this up. >> Also, as a starter in the new language the last thing I want to do is to >> miss out on the language correct way of things and force lets say the Java >> way of doing things in JS. >> >> Can you show me a skeleton of such function and a tiny consumer of that? >> The reason I say that is the fact that all of the examples are all done in >> the same JS file and not setup as module and when I try to change to a >> module and a consumer, I run into syntax problems and .... >> >> I do not understand the difference between a class exporting an object as >> opposed to a constructor >> >> >> Thank you so much for showing me the correct way >> On Thursday, November 21, 2013 7:37:32 AM UTC-8, Kamil Leszczuk wrote: >>> >>> Aah, nevermind then, I misunerstood ;) >>> >>> 21 lis 2013 16:36 "Gregg Caines" <[email protected]> napisaĆ(a): >>>> >>>> Yeah... that's what I'm saying :) >>>> >>>> G >>>> >>>> >>>> On Thu, Nov 21, 2013 at 7:33 AM, Kamil Leszczuk <[email protected]> >>>> wrote: >>>>> >>>>> > For example, if I have a module for emailing with a send() method on >>>>> > it, I don't have it export a constructor; I have it export an object. >>>>> > That >>>>> > object might maintain some state or it might not. When the module is >>>>> > subsequently require()'d, it will have any state that it has accumulated >>>>> > since. >>>>> >>>>> For.most of the time, that's unnecessary - multiple require() calls for >>>>> the same module return same, cached module, so you can store state just by >>>>> using local variables in that module. >>>>> >>>>> -- >>>>> -- >>>>> Job Board: http://jobs.nodejs.org/ >>>>> Posting guidelines: >>>>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines >>>>> You received this message because you are subscribed to the Google >>>>> Groups "nodejs" group. >>>>> To post to this group, send email to [email protected] >>>>> To unsubscribe from this group, send email to >>>>> [email protected] >>>>> For more options, visit this group at >>>>> http://groups.google.com/group/nodejs?hl=en?hl=en >>>>> >>>>> --- >>>>> You received this message because you are subscribed to a topic in the >>>>> Google Groups "nodejs" group. >>>>> To unsubscribe from this topic, visit >>>>> https://groups.google.com/d/topic/nodejs/GmUto9AN47U/unsubscribe. >>>>> To unsubscribe from this group and all its topics, send an email to >>>>> [email protected]. >>>>> For more options, visit https://groups.google.com/groups/opt_out. >>>> >>>> >>>> -- >>>> -- >>>> Job Board: http://jobs.nodejs.org/ >>>> Posting guidelines: >>>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines >>>> You received this message because you are subscribed to the Google >>>> Groups "nodejs" group. >>>> To post to this group, send email to [email protected] >>>> To unsubscribe from this group, send email to >>>> [email protected] >>>> For more options, visit this group at >>>> http://groups.google.com/group/nodejs?hl=en?hl=en >>>> >>>> --- >>>> You received this message because you are subscribed to the Google >>>> Groups "nodejs" group. >>>> To unsubscribe from this group and stop receiving emails from it, send >>>> an email to [email protected]. >>>> For more options, visit https://groups.google.com/groups/opt_out. > > -- > -- > Job Board: http://jobs.nodejs.org/ > Posting guidelines: > https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines > You received this message because you are subscribed to the Google > Groups "nodejs" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/nodejs?hl=en?hl=en > > --- > You received this message because you are subscribed to the Google Groups > "nodejs" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/groups/opt_out. -- -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en --- You received this message because you are subscribed to the Google Groups "nodejs" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/groups/opt_out.
