Geeks With Blogs

News ASP.net Forms Justin Hoffman
Justin Hoffman

Recently, while browsing Visual Studio extensions I came across the SharpKit tool.  It sparked my curiosity as I'm always looking for ways to improve my jQuery and Javascript in general.

Working off the example in my previous post about Updating the Content of a WebGrid async we can generate the same Javascript via the SharpKit project.  First, head to theSharpKit site to download and setup your first project (check out the Screencasts).  Or search "jQuery" in Visual Studio 2010 extension manager.

 So if we take the realodGrid function we can write it in C# with all the great benefits that come with C# coding.

 using  System.Reflection;
 using  SharpKit.JavaScript;
 using  SharpKit.Html4;
 using  SharpKit.jQuery;
 using  SharpKit.jQuery.UI;
 namespace  SharpKitLibrary
 {
     [JsType (JsMode .Global, Filename = "res/scriptFile.js" )]
     public  class  DataMapScripts  : jQueryContext 
     {
         static  void  loadGrid()
         {
             var  settings = new  AjaxSettings 
                                {
                                    url = "/Home/ReloadGrid/" ,
                                    cache = false ,
                                    data = "{}" ,
                                    dataType = "html" ,
                                    success = fillGrid
                                };
             jQuery .ajax(settings);
         }
 
         static  void  fillGrid(object  o, JsString  data, XMLHttpRequest  request)
         {
             J("#grid" ).html(data);
         }
     }
 }
 

The when you Build your SharpKit project, it will output the Javascript file for you.  Here is the result:

 //@AutoGenerated 
 function  loadGrid()
 {
 	var  settings = {url:"/Home/ReloadGrid/" ,
 	cache:false ,
 	data:"{}" ,
 	dataType:"html" ,
 	success:fillGrid};
 	$.ajax(settings);
 }
 function  fillGrid(o, data, request)
 {
 	$("#grid" ).html(data);
 }
 
 
Posted on Friday, April 8, 2011 2:16 PM | Back to top


Comments on this post: Writing jQuery in C# with SharpKit

# re: Writing jQuery in C# with SharpKit
Requesting Gravatar...
I'm going to agree with the above commenter, this seems dangerous. I think a better solution would be to look for areas where you can use reuse/conventions/utility libraries. For example, since I have a lot of similar behavior in all my apps, I've simplified most pages' javascript to just be calls to a common home-brew library, kinda like this:

$.page.useAjaxForms();
$.grid.loadFrom("Products/GridData");
Left by Ryan on Apr 13, 2011 1:36 AM

# re: Writing jQuery in C# with SharpKit
Requesting Gravatar...
@Ryan, what seems dangerous about SharpKit? Granted, every tool like this has its place. I would not recommend this as your single source of Javascripting either... I think my initial thought for SharpKit was that it would be a better Developer Experience than writing normal Javascript with the added bonus of Intellisense. I had not really gone past that.
Left by Justin on Apr 13, 2011 9:15 AM

# re: Writing jQuery in C# with SharpKit
Requesting Gravatar...
Well I just glanced at it, but a few of my concerns would be a) you have to write your own "header" files for 3rd party libraries. b) Javascript is prototype-based and C# is not so you can't really leverage Javascript the way it's meant to be used (granted if you really want to use those language features you probably know quite a bit of JS anyway). c) code generation libraries work well until you try to go beyond their limits, then the amount of extra work often doesn't pay off vs just writing it from scratch in the first place.

If you're looking for a better javascript experience you might want to check out CoffeeScript.

http://jashkenas.github.com/coffee-script/
Left by Ryan on Apr 15, 2011 2:58 AM

# re: Writing jQuery in C# with SharpKit
Requesting Gravatar...
This is a cool feature. However, I think if we get the ability to implement jQuery coding style (selection => action) in C#, then we would really get more productive. Imagine that instead of LINQ to XML, you simply select XML tags using selectors and append children to them.
Left by Saeed Neamati on Jun 20, 2012 12:06 AM

Your comment:
 (will show your gravatar)


Copyright © Justin Hoffman | Powered by: GeeksWithBlogs.net