views:

55

answers:

1

I use this function in javascript to call web service (WCF):

function Do(caller)
{
   MyService.GetClient(some_id, CallBackGetClient);
}

function CallBackGetClient(WebServiceResult)
{
   var result = WebServiceResult;
   etc.
}

Now I want to pass parameter (caller) to CallBackGetClient function so I can make dictinction who called Do() function. How to pass a parameter?

+1  A: 

You can put put it inside an anonymous function, for which you use the same function signature as the expected callback.

function Do(caller)
{
   MyService.GetClient(some_id, function(webServiceResult) {
       CallBackGetClient(webServiceResult, caller);
   });
}

Here you are passing an anonymous function to GetClient as the callback. That function will take the expected web service result as its argument, but inside that function, you're turning around and invoking the real CallBackGetClient, adding caller as a second argument. This works because caller gets "trapped" inside the scope of the anonymous function so it'a still usable.

And this right here is the best thing about Javascript.

quixoto
How then get both two in CallBackGetClient ?
jlp
Not sure what you're asking.
quixoto
CallBackGetClient will look like this: CallBackGetClient(WebServiceResult, caller) then?
jlp
I think I understand what you're after. I updated the answer. Hope that helps.
quixoto