I'm not 100% sure of your requirements, but this is what I assume you're
after:

-------------------------------------------------
file : soapClient.js

var memoizedClient;
var soap = require('soap');
var create = function (cb){
  if (memoizedClient){
    return cb(null, memoizedClient);
  }
  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) {
      return cb(null, memoizedClient);   // pass errors properly! :)
    }
    setSecurity(new soap.BasicAuthSecurity('admin-priv', 'password'));
    memoizedClient = result;
    cb(null, result);
  });
exports.create = create;
exports.client = memoizedClient;  // I don't do this, but I think it's what
you're looking for

---------------------------------------------
file: someClientCode.js

var client;
require('soapClient').create(function(err, client){
  // do stuff here
});
---------------------------------------------

ALSO:
If you're somehow sure that you've already called create() previously, you
could just do:


var client = require('soapClient').client;
// do stuff here

I know that's a hell of a lot prettier, but I usually don't do that because
I like to not
have to worry about whether i've called create() or not.  I just call
create() everytime, and
suck up the fact that there's a callback.  In the cases that I'd use this
pattern like you are
using it, it's ONLY because I'd like to not have to incur the network cost
of the asynch call
everywhere I use it, and not because I want fewer indentations in my code.
I do this with
database connection pools as well, for example, because those shouldn't be
created over
and over for every use.  So in short, ALL of this hinges on my assumption
that
"soap.createClient" involves a network call that you don't want to repeat
everytime you
want to make a soap request.  Otherwise, there's no good reason to memoize
(cache state).

Often I put a setter on the memoizedClient as well because it works nicely
for dependency
injection in unit tests.

Soooo... I think I have to admit in retrospect that this is indeed a
singleton.  At least this
way you can be more idiomatic than any general javascript singleton code
I've seen.

(I never ran any of this code, so it's probably broken in 14 ways)

Hope that helps,
G



On Thu, Nov 21, 2013 at 9:03 AM, Reza Razavipour
<reza.razavip...@gmail.com>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" <gr...@caines.ca> napisaƂ(a):
>>>
>>>> Yeah... that's what I'm saying :)
>>>>
>>>> G
>>>>
>>>>
>>>> On Thu, Nov 21, 2013 at 7:33 AM, Kamil Leszczuk <kami...@gmail.com>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 nod...@googlegroups.com
>>>>> To unsubscribe from this group, send email to
>>>>> nodejs+un...@googlegroups.com
>>>>> 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
>>>>> nodejs+un...@googlegroups.com.
>>>>> 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 nod...@googlegroups.com
>>>> To unsubscribe from this group, send email to
>>>> nodejs+un...@googlegroups.com
>>>> 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 nodejs+un...@googlegroups.com.
>>>> 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 nodejs@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+unsubscr...@googlegroups.com
> 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
> nodejs+unsubscr...@googlegroups.com.
> 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 nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
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 nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to