Welcome to the last article in a three-part series on Aptana, Jaxer's server-side
JavaScript Application Framework. In the Create a File-based
Blog using Jaxer article, we extended our Jaxer file-based blog in Aptana
Studio by including the ability to add posts and save them to a file on the
server. Today, we'll be looking at data persistence in Jaxer as we create a session
variable and move to storing the posts in a proper database.
Session Variables
The init()
function can be modified to use a session variable for the posts
folder. Short term variable persistence can be problematic in web apps due to
the stateless nature of web transactions. To overcome this limitation, server
languages usually incorporate a mechanism for storing session state. Jaxer
manages state information in the Jaxer.Container
class. You
the developer don't interact through the Container
API directly. Rather, you
can treat each state as a JavaScript associative array. Jaxer supports the following
four session contexts:
Jaxer.sessionPage:
Values are stored in the private context
of a single page and belong to a specific session, and as such will endure over
the entirety of that session, expiring when the user's session expires.
Jaxer.session:
A session is automatically created within the
context of an application, expiring when the user's session expires. Session
containers are not shared among applications so if a user is working with two
distinct Jaxer web applications, that user has a separate session container
for each application.
Jaxer.page:
Values persist within the private context of
a single page and shared within the application and among sessions. This is
analogous to a static member of a class in Java; the container belongs to the
page yet the values are static within the application. This container only expires
when the Jaxer server is shutdown.
Jaxer.application:
Stores values accessible within a single
application. The application container is always available when any user accesses
a page belonging to an application. An application container might be used to
cache shared, rarely changed data originally loaded from disk or a database;
your application code is responsible for managing the cache and updating the
session as necessary. This container and its values expire only when the Jaxer
server is shutdown.
We will use the page context because all application sessions will share the
same folder. To obtain the path to the posts
folder, we can use the Jaxer.request.documentRoot
property, which is the path to the project's root web documents folder. By default,
Jaxer sets the working directory to: <Aptana Studio Installation Directory>/.metadata/.plugins/com.aptana.ide.framework.jaxer/jaxer_studio_data/
.
We only have to create it once, so there is a check for its existence using
the NOT bang operator (
local_jaxer/data/DEFAULT!
) in conjunction with the in
operator. It reads: "If
NOT key in Page session object then store it":