how to pass multiple arguments to onSuccess function in Prototype?

感情迁移 提交于 2019-12-10 07:41:29

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/1176257/how-to-pass-multiple-arguments-to-onsuccess-function-in-prototype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!