如何将多个参数传递给Prototype中的onSuccess函数?

时间:2022-12-07 23:16:39

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:-

我是使用Prototype库的初学者。我想知道如何将多个参数传递给Prototype中的onSuccess / onFailure函数?例如:-

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

In my success function fnSccs:-

在我的成功函数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.

我想将一个新参数传递给fnSccs函数。怎么可能。谢谢你的帮助。

1 个解决方案

#1


7  

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.

会发生什么事情,当你调用mySuccess(...)时,会返回旧函数,但是你仍然可以访问参数,因为变量仍然在外部闭包上分配。

You can check the running snippet here.

您可以在此处查看正在运行的代码段。

#1


7  

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.

会发生什么事情,当你调用mySuccess(...)时,会返回旧函数,但是你仍然可以访问参数,因为变量仍然在外部闭包上分配。

You can check the running snippet here.

您可以在此处查看正在运行的代码段。