Data Persistence in Jaxer / Page 2 | WebReference

Data Persistence in Jaxer / Page 2


[prev]

Data Persistence in Jaxer [con't]

Define Your Own Database Connection

More likely than not, you'll want to manage your own database connection(s) for each application. We'll do that here and set up our blog app to run against a MySQL database. You can download it from the MySQL site if you need to.

I find it beneficial to group all global application-level variables to a separate file. In Aptana, there are several ways to create a new JavaScript file. One is to click on the down arrow beside the New button while the editor pane is active and select the "New Untitled JavaScript File" from the menu (Figure 1). This action will open a new file in the editor. You'll have to name it and choose a directory when you save it. Alternatively, you can right-click anywhere in the Project pane and select New=>JavaScript File from the menus ( Figure 2) to bring up the "New JavaScript File" dialog. Enter "global_vars.js" for the file name and click "Finish" to create the file in the root directory ( Figure 3). In a larger project, you might want to create a "scripts" directory to store them.

The following code would place a global variable for the posts directory and connection parameters in an object named Globals:

Next, we have to add a script tag to the blog.html page. To do that, place the cursor where you want to insert the new script tag (E.G.: below the existing two external scripts) and click the "Insert an External JS Tag" button ( Figure 4). Then enter "global_vars.js" for the script name and "server" for the runat attribute ( Figure 5). After the new tag appears in the editor, enter the "autoload="true"" so that the script is guaranteed to be loaded before the serverload event. Here is the resulting code:

This code in the serverload event, creates the blog database, defines the post table, and selects all the posts contained in it. Don't be afraid of the createDB() function. It's smart enough to not clobber the blog database once it's been created. The resultset.toHTML() function formats the rows as an HTML table. Since it also returns column headers, I set its name to a non-breaking space ( ), as we don't want it displayed:

We only need to change one line in the formatPosts() function, which is to remove the call to makeHTML(), as the results were already formatted above:

The save() function only requires a connection object and insert statement to add the post to the database:

And just like that, you are now using a database to manage your blog posts! Here's the files that we worked on today:

And here's more information on working with databases using Jaxer:


Rob Gravelle combined his love of programming and music to become a software guru and accomplished guitar player. He created systems that are used by Canada Border Services, CSIS and other Intelligence-related organizations. As a software consultant, Rob has developed Web applications for many businesses and recently created a MooTools version of PHPFreechat for ViziMetrics. Musically, Rob recently embarked on a solo music career, after playing with Ivory Knight since 2000. That band was rated as one Canada's top bands by Brave Words magazine (issue #92) and released two CDs. Rob's latest, entitled KNIGHTFALL, was a collaboration between himself, the former Ivory Knight vocalist, and legendary guitarist/producer, Jeff Waters of Annihilator fame. Rob is available for short-term software projects and recording session work. to inquire, but note that, due to the volume of emails received, he cannot respond to every email. Potential jobs and praise receive highest priority!

Original: September 4, 2009


[prev]