views:

752

answers:

1

Hi,

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?');}" })%>
+3  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>

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

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

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:

  • Write a function to be invoked in the OnBegin.
  • Inside this function, access the original triggerElement, and add it as a property to the original ajaxOptions (which can be accessed, too)
  • Then, in the OnSuccess function, access your hacked property of ajaxOptions.
Ron Klein
Ahh I didn't know about the existence of .caller. I'm unable to test until monday, but I think you are spot on. I'm gonna boost your rep! Thanks!
Ropstah