Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass multiple arguments to onSuccess function in Prototype?

I am a beginner in using Prototype library. I want to know how we can pass multiple arguments to onSuccess/onFailure function in Prototype? For example:-

new Ajax.Request('testurl',{
        method: 'post',
        parameters: {param1:"A", param2:"B", param3:"C"},
        onSuccess: fnSccs,
        onFailure: fnFail
        })

In my success function fnSccs:-

function fnSccs(response)
{
    alert(response.responseText);
}

I want to pass a new argument to fnSccs function. How is that possible. Thanks for any help.

like image 993
ajithmanmu Avatar asked Jul 24 '09 07:07

ajithmanmu


People also ask

What are the two different ways to pass arguments into functions?

There are two ways to pass parameters in C: Pass by Value, Pass by Reference.

How many ways we can pass arguments to function?

Parameters can be passed to a method in following three ways : Value Parameters. Reference Parameters. Output Parameters.

How do you pass an argument to a JavaScript?

Arguments are Passed by Value The parameters, in a function call, are the function's arguments. JavaScript arguments are passed by value: The function only gets to know the values, not the argument's locations. If a function changes an argument's value, it does not change the parameter's original value.


1 Answers

You could wrap your success function into another one that recieves the desired parameter, and returns your old function back:

new Ajax.Request('testurl',{
                method: 'post',
                parameters: {param1:"A", param2:"B", param3:"C"},
                onSuccess: mySuccess('myValue1', 'myValue2'),
                onFailure: fnFail
                })

function mySuccess(param1, param2){
  return function(response){ // Your old success function
    alert(param1);  // The parameter still accessible here
    alert(param2);
    alert(response);
  }
}

What happens is that when you call mySuccess(...) your old function is returned, but you still have access to the parameters because the variables remain allocated on the outer closure.

You can check the running snippet here.

like image 105
Christian C. Salvadó Avatar answered Sep 27 '22 19:09

Christian C. Salvadó