Single Page AIR Applications with jQuery

Filed under: AIR, jQuery

comments (4) Views: 7,206

Quite a few people have asked how to create AIR applications that utilize the "one page" approach. There are many benefits to this method, most importantly variable scope. In AIR when you switch from one page to another you lose the data you've already got loaded onto the current page. With a single page approach, you're making external page calls via AJAX to load in parts of pages, keeping all current data available to the current page and new pages as well. Using jQuery it couldn't be simpler. Let's see how it works. But first take a minute to download the Single Page AIR Applications source for your reference.

Create a new AIR project and some additional pages. We'll be loading the extra pages in using jQuery's $.load() method. Before we move on let's take a quick look at how the $.load() method works.

The load method is generally attached to a container, in the example above, that's one with an id of containerToLoadInto. The load method will look for a page called test.html. Now here's the cool part...jQuery will load in that page, and if the optional target search param, #sourceContainer, is found it will extract that container (and it's contents) and place them inside the containerToLoadInto container. Finally, there's an optional callback function available for your use.

To honor the pending arrival of my third child the project has a pregnancy theme, with information taken from Wikiepedia's article on pregnancy. In this example I have a list of links I'm using as navigation. When the link is clicked I'm extracting the href from the anchor tag and passing it to the load method.

This couldn't be simpler, but it can be more robust as well. One thing this method doesn't offer is bookmarking. Now that's probably not as important in desktop applications, but it could be useful. There are ways of approaching this in code, but if you're looking for more robust routing support, then I'd recommend looking into Sammy. It bills itself as a "RESTful Evented JavaScript" and built on top of jQuery. If you're already familiar with languages like Ruby that offer routing based on the URL then Sammy is the JS framework for you.

Amazon logo

If this article was interesting, or helpful, or even wrong, please consider leaving a comment, or buying something from my wishlist. It's appreciated!

comments powered by Disqus
coach outlet online jordan 13 grey toe beats by dre cyber monday michael kors black friday beats by dre cyber monday jordan 6 black infrared north face cyber monday michael kors cyber monday north face black friday coach outlet black infrared 23 13s north face cyber monday jordan 6 black infrared north face black friday coach cyber monday jordan 11 legend blue north face cyber monday black infrared 6s lebron 12 north face black friday jordan 11 legend blue louis vuitton outlet jordan 13 grey toe grey toe 13s beats by dre black friday coach black friday jordan 13 grey toe coach cyber monday uggs black friday jordan 13 black infrared 23 uggs cyber monday barons 13s uggs black friday beats by dre cyber monday black infrared 6s jordan 13 bred jordan 13 black infrared 23 north face black friday black infrared 6s jordan 11 legend blue michael kors black friday jordan 13 grey toe coach black friday michael kors black friday michael kors cyber monday beats by dre cyber Monday north face cyber monday coach black friday michael kors cyber monday beats by dre cyber Monday north face black friday beats by dre black friday lululemon black friday uggs black friday jordan 13 bred coach cyber monday beats by dre black friday uggs black friday coach black friday black infrared 6s