Aptana Jaxer: MVC and other thoughts

Tags:

This week I decided to take a look at Aptana Jaxer – A server-side JavaScript implementation, or “Ajax Server” like their site says. It’s a bit different take on a server-side language and requires some new ways to do things.

What it basically lets you do, is completely throw away all “traditional” server-side languages, and write all your code in JavaScript. What’s more, it lets you edit the DOM of the page using the same means you would use to edit it in the browser client-side.

Initial expectations

Looking at Jaxer’s site, my initial concern was that you would end up with pretty poor code. Everyone with some experience probably has seen their fair share of HTML mixed with PHP, HTML mixed with ASP etc., and the examples on the site provided nothing else.

Despite this, I thought it was a good idea to look further. The samples don’t always tell you enough, and the idea of maybe developing everything in JavaScript is indeed quite interesting. For example, if you had some form validation code and wanted it to work on both the client and the server, with Jaxer, you could just write the JS validators once and make them run both on the client and server.

A new way to look at things

For someone like me who’s been doing a lot of work with PHP, wrapping your head around Jaxer’s ways seems a bit difficult. If you think of the MVC model for example, you would have a front controller which parses the request, calls a controller and then presents the view.

Jaxer always has the initial document. When you load a page, you need some basic HTML and JavaScript to make anything happen. At that point you already have some HTML. Then your code can maybe add some data to the HTML from the DB or something, but loading completely new snippets of HTML, similar to how you would use views, is a bit more complicated.

For loading files, Jaxer provides some file IO methods. You could use that to load some new HTML, but you can’t construct a new DOM document from that loaded code. If you could actually stick the newly loaded HTML into a separate document, you could achieve a separate view better. Currently you can just stick that code into an element in the document and then muck with it.

Simple MVC in Jaxer

I thought I’d need to give Jaxer some proper testing, so I wrote a simple MVC “framework”. It’s very simple, so that’s why the quotes. ;)

It has some basic functionality:

  • Can display a master layout
  • Lets you separate code to controllers and actions and calls them based on some URL parameters
  • Has a basic “view” feature

So it’s really barebones, but it works and proves the point: Yes, you can actually bend Jaxer to MVC.

If you want to take a look at the code, by following these links you can see the main page / “front controller”, the main “framework” javascript, the controllers and the views. All the code is visible by looking at the pages you can find at those URLs, even the server-side code.

Please don’t bash the code too much; I wrote it pretty quickly! :D
I also added some commenting to it, to hopefully make it easier to understand.

Conclusion

While not a new concept, server-side JavaScript in the form of Jaxer seems quite promising to me. The technology is usable even right now, but I don’t know about the performance and such.

I don’t see how a language like PHP would benefit from Jaxer-powered view layer as is suggested on their website, though.
I also think there could be a better approach than the MVC example I showed for making apps with Jaxer, since you now have good access to the DOM. What it is, I don’t know, not yet at least.

I have yet to see any “proper” website using Jaxer, but I think it is already feasible.