如何传递多个参数的onSuccess函数原型? [英] how to pass multiple arguments to onSuccess function in Prototype?

查看:1152
本文介绍了如何传递多个参数的onSuccess函数原型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初学者在使用原型库。我想知道我们如何能够在原型传递多个参数的onSuccess / onFailure功能? 例如: -

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
		})

在我成功的功能fnSccs: -

In my success function fnSccs:-

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

我想通过一个新的参数fnSccs功能。这怎么可能。感谢您的帮助。

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

推荐答案

您可以换你的成功的功能到另外一个,临危所需的参数,并返回旧的机能恢复:

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);
  }
}

什么情况是,当你调用 mySuccess(...)您的旧函数返回,但你仍然可以访问参数,因为变量保持在分配外关闭

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.

这篇关于如何传递多个参数的onSuccess函数原型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆