Hire me at Go Tripod.

Javascript Generation – A Change Of Heart

April 2nd 2008, 3:03 pm in .NET, C#, Development, Ext, Javascript.

I’ve previously talked about my dislike for code generation in Javascript. I still standby that, but I am beginning to have a change of heart for certain scenarios. I’ve been working on a couple of ExtJS applications recently and I’ve got lots of code which is purely ExtJS stuff on its own – no HTML or other code in there really. It’s been a project where I’ve been using C# 3 and it’s partly the similarities between the two languages which have triggered this rethink.

Take a look at this code:

var win = new Ext.Window({
	title: 'Order Viewer', layout: 'border',
	width: 500, height: 500,
	modal: true, resizable: false, closable: false, draggable: false,
	items: [ frm, lst ]
});

win.on('render', function() {
	load(5);
});

win.show();

That’s pretty standard stuff for what I’m doing with Ext right now – I create a new window with a load of configuration options, then add in an event handler for the window’s render event, and finally show the window on screen. Now imagine that you could write the following in C# 3:

var win = new Ext.Window{
	Title = "OrderViewer", Layout = Layout.Border,
	Width = 100, Height = 200,
	Modal = true, Resizable = false, Closable = false, Draggable = false,
	Items = new [] { frm, lst }
};

win.Render += delegate {
	load(5);
};

win.show();

As far as I’m aware, all of the above is valid C# 3 code. I’ve basically written it as I’d like to use it, with an enum for the Layout type, and C# style event handler syntax, but the interesting thing to me is how very similar it is to the Javascript version.

The ExtSharp project allows you to write your Ext code in C# and have it rendered as Javascript. I’m going to explore it in my next post, and examine how closely it can be made to fit the code sample above.

Responses to “Javascript Generation – A Change Of Heart”

Comments are closed.

  1. There is a similar project that does compile C# binary to javascript and actionscript at http://jsc.sf.net

    Check it out aswell.

    zproxy
  2. This’ll come in handy, when my company eventually goes over to ASP — thanks!

    mdmadph
  3. [...] C# ve javascript iÅ?birlirÄ?ini saÄ?lamak. ExtSharp BaÄ?lantı [...]

    Fatih HayrioÄ?lu’nun not defteri » 08 Nisan 2008 web’den seçme haberler
  4. http://www.aybardumlu.com

    Aybar