views:

356

answers:

3

I've tried to inspect the AjaxContext that ASP.NET-MVC uses on for instance Ajax Actionlinks and their onSucces, onComplete etc. clientside functions. But I don't understand very much of it... Where is the documentation on this thing?

Does anybody know how to I get the 'target' or 'srcElement' (e.target or window.event.srcElement) when I am in a onSuccess or onComplete javascript event?

<%=Ajax.ActionLink(
"LinkText", "Action", New With {.Controller = "ControllerName"}, 
New AjaxOptions With {
    .UpdateTargetId = "divElement", 
    .OnSuccess = "function(ajaxContext) {console.log(ajaxContext);}"
}) %>

Which results in:

<a 
    href="/Popout/ApplicationCodePopout"
    onclick="Sys.Mvc.AsyncHyperlink.handleClick(
        this, new Sys.UI.DomEvent(event), 
        { 
            insertionMode: Sys.Mvc.InsertionMode.replace, 
            updateTargetId: 'divElement', 
            onSuccess: Function.createDelegate(this, 
                function(ajaxContext) {console.log(ajaxContext);}
            )
        }
    );"
>LinkText</a>
+1  A: 

Okay so you need to get Firebug installed (If you havnot already done so do it now :) Now start using console.log in your code to help you find out what properties and functions each object has available. Try typing in console.log(document) - You can do this in the Console window in the textbox (next to the >>>). Notice how you can click on the links in the console to interigate and see what properties and functions the object has available.

Jake Scott
Remember to remove the console.log statements after you get it working too! Internet explorer and other browsers do not support console.log as it is part of firebug
Jake Scott
or just do `console ` to only write to the console if it exists.
svinto
Ok, I already inspected it using some javascript function, this is a bit the same... Still I can't find any e.target or e.srcElement anywhere...?
Ropstah
can you post your javascript code, might be a little easier if we can look at what your doing
Jake Scott
ok, see updated post
Ropstah
+2  A: 

You could change the onSuccess method to this:

<%=Ajax.ActionLink(
"LinkText", "Action", New With {.Controller = "ControllerName"}, 
New AjaxOptions With {
    .UpdateTargetId = "divElement", 
    .OnSuccess = "function(ajaxContext) {debugger;}"
}) %>

(Note the debugger keyword)

Then you can break into it using VS2008 (assuming IE, if you're using Firefox, then install Firebug as Jake said) and then you can use the quick watch window (in VS or equivalent in Firebug etc) to view the object and it's properties/ methods etc.

As for some documentation, check out this link to see the code comments, and this article for some more information.

Kieron
+1 Yeah what he said
Jake Scott
A: 

I would also recommend reading jQuery in action. One of the best tech books I have ever read! Once you read that you will be able to start writing your own jQuery code and not have to worry about using a server side wrapper library that spits out javascript for you :)

Check it out http://www.scribd.com/doc/8635225/jQuery-in-Action

Jake Scott