13

I want to something similar to this:

function AjaxService()
{

 this.Remove = function (id, call_back)
 {
    myWebService.Remove(id, CallBack)
 }

 function CallBack(res) {
        call_back(res);
    }  
}

so my calling program will be like this:

var xx  = new AjaxService();
xx.Remove(1,success);

function success(res)
{


}

Also if I want to add more parameters to success function how will I achieve it. Say if I have success function like this:

var xx  = new AjaxService();
//how to call back success function with these parameters
//xx.Remove(1,success(22,33));

function success(res,val1, val2)
{


}

Help will be appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Parminder
  • 3,088
  • 6
  • 37
  • 61

3 Answers3

23

Use a closure and a function factory:

function generateSuccess (var1,var2) {
    return function (res) {
        // use res, var1 and var2 in here
    }
}
xx.Remove(1,generateSuccess(val1,val2));

What you're passing here is not the generateSuccess function but the anonymous function returned by generateSuccess that looks like the callback expected by Remove. val1 and val2 are passed into generateSuccess and captured by a closure in the returned anonymous function.

To be more clear, this is what's happening:

function generateSuccess (var1,var2) {
    return function (res) {
        // use res, var1 and var2 in here
    }
}
var success = generateSuccess(val1,val2);
xx.Remove(1,success);

Or if you prefer to do it inline:

xx.Remove(1,(function(var1,var2) {
    return function (res) {
        // this is your success function
    }
})(val1,val2));

not as readable but saves you from naming the factory function. If you're not doing this in a loop then Xinus's solution would also be fine and simpler than my inline version. But be aware that in a loop you need the double closure mechanism to disconnect the variable passed into the callback function from the variable in the current scope.

slebetman
  • 109,858
  • 19
  • 140
  • 171
  • 2
    there are less complicated ways to do this in javascript, don't you think? – jrharshath Jan 04 '10 at 04:58
  • @parminder Are you sure it doesn't work? This is the standard way to pass arguments to callbacks that doesn't accept arguments, for example passing arguments to `setTimeout`. Is there something you may have missed form the syntax? – slebetman Jan 04 '10 at 06:16
  • 2
    @harshath.jr There's nothing complicated about this. It's pretty standard JavaScript – Justin Johnson Jan 04 '10 at 06:34
8

You can pass it as anonymous function pointer

xx.Remove(1,function(){
                           //function call will go here
                           success(res,val1, val2);
                      });
Xinus
  • 29,617
  • 32
  • 119
  • 165
0

one way to do this:

function AjaxService {
    var args_to_cb = [];
    this.Remove = function (id, call_back, args_to_callback_as_array) {
        if( args_to_callback_as_array!=undefined )
            args_to_cb = args_to_callback_as_array;
        else 
            args_to_cb = [];
        myWebService.Remove(id, CallBack)
    }

    function CallBack(res) {
       setTimeout( function(){ call_back(res, args_to_cb); }, 0 );
    }  
}

So you can use it like this:

var service = new AjaxService();
service.Remove(1,success, [22,33]);

function success(res,val1, val2)
{
    alert("result = "+res);
    alert("values are "+val1+" and "+val2);
}

I usually have the callback execute using a setTimeout. This way, your callback will execute when it gets the time to do so. Your code will continue to execute meanwhile, e.g:

var service = new AjaxService();
service.remove(1, function(){ alert('done'); }); // alert#1
alert('called service.remove'); // alert#2

Your callback will execute after alert#2.

Of course, in case of your application, it will happen so automatically since the ajax callback itself is asynchronous. So in your application, you had better not do this.

Cheers!
jrh

Justin Johnson
  • 30,978
  • 7
  • 65
  • 89
jrharshath
  • 25,975
  • 33
  • 97
  • 127