获取对onSuccess函数中的'triggerElement'的引用? [英] Get reference to 'triggerElement' in onSuccess function?
问题描述
是否可以引用在 onSuccess
函数中调用Ajax请求的 triggerElement
? / p>
Is it possible to get a reference to the triggerElement
that invoked the Ajax request in the onSuccess
function?
<%=Ajax.ActionLink("x", a, r, New AjaxOptions With {.OnSuccess = _
"function(context) {alert('get triggerElement reference here?');}" })%>
推荐答案
页面呈现为:
< a href = /<任何内容> /< action> onclick = Sys.Mvc.AsyncHyperlink.handleClick (此,新的Sys.UI.DomEvent(event),{insertMode:Sys.Mvc.InsertionMode.replace,onSuccess: Function.createDelegate(this,function(context){alert('在这里获取触发元件参考吗?');})});> x< / a>
The page is rendered to:
<a href="/<whatever>/<action>" onclick="Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace, onSuccess: Function.createDelegate(this, function(context) { alert('get triggerElement reference here?'); }) });">x</a>
因此,让我们看一下 Scripts\MicrosoftMvcAjax.debug.js
中的 Sys.Mvc.AsyncHyperlink.handleClick
:
So let's have a look at Sys.Mvc.AsyncHyperlink.handleClick
inside Scripts\MicrosoftMvcAjax.debug.js
:
Sys.Mvc.AsyncHyperlink.handleClick = function Sys_Mvc_AsyncHyperlink$handleClick(anchor, evt, ajaxOptions) {
/// omitted doc comments
evt.preventDefault();
Sys.Mvc.MvcHelpers._asyncRequest(anchor.href, 'post', '', anchor, ajaxOptions);
}
因此,将ActionLink呈现为锚点( a)一个 onclick事件,该事件使用 Sys.Mvc.AsyncHyperlink.handleClick
(其中 this 作为参数之一)映射到 anchor 。
然后是此 Sys.Mvc.MvcHelpers._asyncRequest
调用,其中以 anchor 作为第四个参数。让我们看看 Sys.Mvc.MvcHelpers._asyncRequest
:
So the ActionLink is rendered to an anchor ("a") tag, with an "onclick" event, which uses Sys.Mvc.AsyncHyperlink.handleClick
with this as one of the parameters, mapped to anchor.
Then there's this Sys.Mvc.MvcHelpers._asyncRequest
call with anchor as the fourth parameter. Let's have a look in Sys.Mvc.MvcHelpers._asyncRequest
:
Sys.Mvc.MvcHelpers._asyncRequest = function Sys_Mvc_MvcHelpers$_asyncRequest(url, verb, body, triggerElement, ajaxOptions) {
/// omitted documentation
if (ajaxOptions.confirm) {
if (!confirm(ajaxOptions.confirm)) {
return;
}
}
if (ajaxOptions.url) {
url = ajaxOptions.url;
}
if (ajaxOptions.httpMethod) {
verb = ajaxOptions.httpMethod;
}
if (body.length > 0 && !body.endsWith('&')) {
body += '&';
}
body += 'X-Requested-With=XMLHttpRequest';
var requestBody = '';
if (verb.toUpperCase() === 'GET' || verb.toUpperCase() === 'DELETE') {
if (url.indexOf('?') > -1) {
if (!url.endsWith('&')) {
url += '&';
}
url += body;
}
else {
url += '?';
url += body;
}
}
else {
requestBody = body;
}
var request = new Sys.Net.WebRequest();
request.set_url(url);
request.set_httpVerb(verb);
request.set_body(requestBody);
if (verb.toUpperCase() === 'PUT') {
request.get_headers()['Content-Type'] = 'application/x-www-form-urlencoded;';
}
request.get_headers()['X-Requested-With'] = 'XMLHttpRequest';
var updateElement = null;
if (ajaxOptions.updateTargetId) {
updateElement = $get(ajaxOptions.updateTargetId);
}
var loadingElement = null;
if (ajaxOptions.loadingElementId) {
loadingElement = $get(ajaxOptions.loadingElementId);
}
var ajaxContext = new Sys.Mvc.AjaxContext(request, updateElement, loadingElement, ajaxOptions.insertionMode);
var continueRequest = true;
if (ajaxOptions.onBegin) {
continueRequest = ajaxOptions.onBegin(ajaxContext) !== false;
}
if (loadingElement) {
Sys.UI.DomElement.setVisible(ajaxContext.get_loadingElement(), true);
}
if (continueRequest) {
request.add_completed(Function.createDelegate(null, function(executor) {
Sys.Mvc.MvcHelpers._onComplete(request, ajaxOptions, ajaxContext);
}));
request.invoke();
}
}
因此,原始的锚点现在是 triggerElement ,但是如您所见,该参数在函数的主体中从未使用。
因此,如果您想使用某种参数
但是,这是JavaScript,因此只要浏览器没有移动到另一个页面,您就可以访问几乎所有内容, >包括调用堆栈。例如:
So the original anchor is now triggerElement, but as you can see, this parameter is never used in the function's body.
So, if you want to have some kind of a "formal" (or documented) reference to triggerElement - no such thing.
But hey, it's JavaScript, so you can access almost anything as long as the browser did not move to another page, including the call stack. For instance:
<script type="text/javascript">
function a(p, q)
{
b();
}
function b() {
var x = arguments.caller[1];
alert(x); // boo!
}
a(789, "boo!");
</script>
因此,最终您可以对其进行破解并访问原始锚点。我建议您执行以下操作:
So eventually you can hack it and access the original anchor. I suggest you do the following:
- 写一个要在
OnBegin中调用的函数
。 - 在此函数内,访问
原始的triggerElement
,然后
添加它作为原始
ajaxOptions
的属性(也可以访问,也可以访问
) - 然后,在
OnSuccess
函数,
访问
ajaxOptions
的被黑财产。
- Write a function to be invoked in the
OnBegin
. - Inside this function, access
the original
triggerElement
, and add it as a property to the originalajaxOptions
(which can be accessed, too) - Then, in the
OnSuccess
function, access your hacked property ofajaxOptions
.
这篇关于获取对onSuccess函数中的'triggerElement'的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!