alanwilliamson

Server side Javascript with CFML

I have always wondered why CFSCRIPT was never Javascript, but instead a half-way house. This is something I have spoken about before in various blogs and forums. I have made no secret of the fact that I have not been a fan of CFSCRIPT and feel it was a lost opportunity.

Javascript has taken the development world by storm, with it now being one of the fastest growing languages out there. In short, there is a lot of Javascript talent out there, and I want them to be able to utilise this talent server-side, within CFML applications.

Also, selfishly, I want it. I have been doing a lot of intense Javascript of late (building JQuery plugins) and have enjoyed working with some of Javascript's constructs. Been frustrated with others, but on the whole it is a wonderfully flexible language that should be freed from the confines of the browser.

So I wanted it at the server side. I wanted to plunder the rich CFML functions within my Javascript classes. I want to run queries from within my Javascript objects, and have the results ready for me there. No nasty XML-RPC or REST calls.

I have been noodling away on some Java code on OpenBD for the last few months and I am now in a position where I would like to show the wider CFML community to get their input. The reason I have kept it quiet is that I wanted to make sure it was 'do-able' before I went off claiming big things. I spoke of this innovation to the Steering Committee a number of months ago, and gathered their feedback.

Enter <CFJS>...</CFJS>

My aim is to give developers the ability to code CFC's and functions in pure Javascript. Import existing Javascript libraries into their CFML apps and have that all run at the server side.

There are many major logistical problems to solve, but on the whole I believe I have made good progress.

So consider the following __working__ example:

<cfjs>
ff = function(){
  return $cf.random();
};

x = $cf.pi();
y = ff();
z = cgi.get("script_path");
</cfjs>

<cfoutput>
x = #x#
y = #y#
z = #z#
</cfoutput>

You write pure Javascript 1.7 inside the CFJS tags. You can declare functions/objects in Javascript and do all the usual things you would do with Javascript.

Note the "$cf" object. This is a window into all the core functions on offer from the underlying CFML language. This lets Javascript play with all the CFML functions and marshall data in and out accordingly.

Any 'global' variables declared in the top level Javascript is now available to your CFML page. I am also working on making the declared Javascript functions available to the CFML page for later processing.

CFML variables available to the Javascript function is proving a little more interesting but not impossible, particularly the complex types such as query objects. As you can see though, the example I have shown illustrates the CFML 'cgi' scope being accessed. Still undecided if this is the right way to go here.

I have been using Mozilla Rhino for this, heavily modifying it for use within OpenBlueDragon. This will be available as a downloadable plugin JAR that I am currently packaging up and will make available within the next day.

Progress will be tracked here, until such points it is of a sufficient quality to be released as an official OpenBD plugin. But for the moment, consider it a testing ground to play around with some ideas and constructs.

I am purposely building this plugin to be completely useable by our Google App Engine port. Vision is to enable Javascript server-side in the cloud!

I am very excited about this development. Bringing Javascript to CFML will allow us to build a whole host of wonderful additions. Imagine, building web-services easily with Javascript, but utilising all the ease at which CFML can house. By levaraging the OpenBD/CFML platform we can start to build out the next generation of web apps with Javascript libraries spanning both client and server side.

More importantly, we are opening up the world of server-side processing to a whole army of new Javascript developers. Exciting times. But got a wee bit to go before we get there.


 

Recent Cloud posts

Recent JAVA posts

Latest CFML posts


 
Site Links