I have an HtmlHelper
extension method that takes javascript callback functions as parameters.. for example:
@Html.SomethingCool("containerName", "jsCallbackFunction")
<script type="javascript">
function jsCallbackFunction(e, i) {
alert(e.target.name + ' / ' + i);
}
</script>
As you can see, the javascript callback function name is passed to the HtmlHelper
extension method. This causes the developer to have to refer back to the documentation to figure out what parameters the jsCallbackFunction
function needs.
I would much rather prefer something like this:
@Html.SomethingCool("containerName", New SomethingCoolCallbackDelegate(Address Of jsCallbackFunction))
<OutputAsJavascript>
Private Sub jsCallbackFunction(e, i)
' SOMETHING goes here. some kind of html dom calls or ???
End Sub
The SomethingCoolCallbackDelegate
would provide the code contract for the target function.
Then the compiler would compile the jsCallbackFunction as javascript on the MVC page.
Is there anything like this built into .NET 4 / ASP.NET MVC 4 / Razor 2 ? Or any other technology that can achieve something similar?
Examples are in VB, but solutions in C# are quite acceptable as well.
Clarification:
@gideon: notice that jsCallbackFunction
takes two parameters e
, and i
. However, the HtmlHelper extension method simply asks for a string (the name of the javascript callback function) and does not indicate what parameters this function might take. The problem I am trying to solve is two-fold.
First, the missing parameter hints. A .NET delegate type passed in place of the "javascript callback name" string would accomplish this. I am open to other solutions to accomplish this. I am aware of XML comments. They are not really a solution.
Second, trying to keep the page programmer working in a single language. Switching between javascript and VB (or js and C#) requires (for me at least) an expensive context switch. My brain doesn't make the transition quickly. Keeping me working in VB or C# is more productive and cost effective. So being able to write a function in a .NET language and have it compiled to javascript, in the context of an ASP.NET MVC/razor view, is what I am after here.
@TyreeJackson: SomethingCool
is an HtmlHelper
extension method that I would write that outputs html and javascript. Part of the javascript output needs to call into a user(programmer)-supplied function to make some decisions. Think of it similar to the success or failure function you supply to an ajax call.
window.alert
in VB? – Postconsonantal@Html.SomethingCool("containerName", "jsCallbackFunction")
– BaranowskistringBuilder.AppendLine(" function jsCallback(arg1, arg2) {")
. – Mcquoid