More Thoughts on Server-Side JS

I got a lot of great comments (along with some not-so-great ones) regarding my server-side JS implementation. As expected, quite a few people were very excited about using AS3 on the server instead of JS, and I agree. It got me thinking about how much code reuse you could get within an application. At the very least, you can share libraries, which is handy. But you can also very likely share VO's, and quite possibly entity objects as well. If you're using AIR, and you're building your server-side in the appropriate ECMAScript, then you could theoretically port your server-side to the client side for use in standalone mode (i.e. persisting to SQLite), and then the "occasionally connected client" model becomes nothing more than two databases that you have to sync. No need to implement everything twice, because you're using the same technology in both tiers.

One big gripe (which I share) with script notation is the lack of tag bodies. CFQUERY and CFMAIL are wonderfully simple to use because their main textual parameter is just the tag's body. Having to synthesize that with string literals as you do in script is cumbersome to say the least. What I realized tonight, however, is that E4X (or ECMAScript for XML) isn't actually tied to ECMAScript 4, it's standalone, so it can be used with ECMAScript 3 implementations (such as Rhino). And that means that you can actually embed XML snippets directly in your code. I haven't tested it (had to work on other stuff this evening), but you should be able to take what I've got today for querying:

var get = new Query("select * from user where email is not null", "mydsn").execute();

and replace it with something like this:

var get = new Query(<query datasource="mydsn"><![CDATA[
select *
from user
where email is not null
]]></query>).execute();

While it's not quite as clean as the CFML version, it's a big step towards it. Better yet, Rhino supports E4X today, so this should work immediately. Not sure about WebKit (AIR's HTML/JS implementation), though a quick Googling indicates that as of this time last year there was nothing available, nor anything in the works. If only they'd used Gecko, which has supported it for a while. Now that I think about it, depending on the E4X implementation, it's entirely possible that you might be able to do this:

var get = <query datasource="mydsn"><![CDATA[
select *
from user
where email is not null
]]></query>.execute();

Doing that would depend on being able to extend the core E4X classes in the style of Prototype's extensions to Object and Array. Just throw an execute method on there that checks the root node of the doc it's called on as a selector for the desired action, and than hands off processing to the right subsystem, and you're done.

So much cool stuff…

Comments are closed.