Server-side jQuery and more cool tricks with Aptana Jaxer
In this example, (the first in a series) we're going to build a simple voting tool, using a single page of DHTML. The implementation is quite basic but covers a good few examples of how to use Aptana Jaxer in real world situations, such as :
- Using Ajax libraries on the server (jQuery in this example)
- Server-side DOM manipulation (using jQuery)
- Storing and retrieving Session data
- Creating and accessing a database
- E4X as a templating mechanism (E4X provides native XML support in JavaScript)
- Handling form data
Let's Vote...
This example was written as a single page webapp. You could remove the Javascript to another file and make it all unobtrusive if that is what gets you excited, but I'm just using an inline script tag, as the code is only about 30 or so lines long. Also in Jaxer we could have easily implemented this using Jaxer's seamless ajax callback mechanism but for the purpose of this example we'll stick with a traditional form post.
Let's get started. Most folks reading this should be familiar with the standard blog/portal poll, where you are presented with a set of choices.
and once you have voted you get to see the current results of the voting.
In our example we allow multiple votes per user but you can easily change that by just commenting out a single line of code.
One of the interesting features of this application is that, by using server-side DOM manipulation in Jaxer, you can remove any unwanted content before it is sent to the client browser. We use this to hide the vote results until the user has voted
This is a useful technique for permission based forms where, for example, you may want remove the credit card details unless the user has established the correct permissions and been validated by the server.
A very basic web page
So let's jump in and have a look at the code used to make this work. The code in this article has been updated to use the API of Jaxer 0.9.8 or later, for The full source listing below contains comments to indicate where the code was changed to work with the later API.
Above is the contents of the HEAD element. Just the usual suspects, setting the title and some simple CSS stuff. The only interesting part is at line #3, where we load the jQuery library on the server, because we intend to do some serverside DOM manipulation before the page is sent to the client.
The runat='server' attribute tells Jaxer to load this javascript library on the server.
The autoload attribute is a recent addition to Jaxer, and it instructs Jaxer to load that page and keep it cached as preparsed bytecode (for faster library loadtime) for any future calls to this page, including callbacks.
In the body of the document we have a simple form which we will dynamically populate on the server. The form will post the vote to itself on the server.
We are marking up the DOM content with the classnames 'voter' and 'nonvoter' to identify content that is specific to a user's status, and make it easily accessible using jQuery on the server.
Server Side Javascript
Now we get to the Javascript. The app has a single script tag in the body which is designated to run on the server.The first dozen or so lines are simply creating a DB and preparing a schema for use.
So we've connected to the database (which was automatically created if needed, how convenient!) and set up the schema and initial content we expect. We also setup an Array (questions) containing the questions for the voting poll
Next we need to check the session value we are storing (status) to determine whether this user has already voted, and then check to see if the form data for the vote is being posted. Then, if we are actually voting, we write the vote to the database and set the user status to 'voter'.
When we write the vote to the database, we grab the sessionID and the remote IP address and write those out with the vote data, this will let us enforce single voting later if we need it.
Finally query the database to get the current vote counts, remembering to subtract 1 from the total to account for the dummy rows we inserted to seed database and prevent any nulls from appearing in the results totals.
Now we build the DOM, to do this we issue a query to the DB to get the current vote tally.
Using E4X - ECMAScript For XML as a simple templating tool we create the DOM with the nodes populated according to our dataset.
One of the nice features of server-side javascript with Jaxer is that you have access to all neat things built into Mozilla without the worry of client side browser support, which enables reliable use of the advanced features of the Javascript language.
If you look closely at the code below you'll notice we use a simple syntax for variable substitution using curly braces containing javascript code inside the E4X assigments. This allows us to use this for templating as long as we are creating valid XML nodes.
So the document now has a DOM that has been populated with the content for BOTH voters and non-voters. We use a little jQuery magic to remove the elements we don't want presented to the user.
In this way the user will EITHER see the form with the question and the submit button
OR the current voting results data.
Now we set the session variable status to be the current status of the user as they have either voted or not.
Finally as the page that is served has no further dependency on Jaxer once it leaves the server, we tell Jaxer to not bother injecting the client framework. Normally the client framework would be automatically inserted as a script tag in the outgoing HTML, but our simple example doesn't need to communicate back to the Jaxer server, as it contains no server callbacks.
So there you have it, a simple poll on a single page, using many of Jaxer's cool features.
Supporting Files
The full code and supporting files for this article are available here

