We whip up a small sample app to see just how much heavy-lifting jQuery takes off a mobile developer's hands.
Last week we introduced JQuery Mobile as a new option for mobile software development. JQuery Mobile is from the same team that has produced the popular JQuery Project.
In this article we take a look at a basic JQuery Mobile-based application.
The example code for this article is hosted at http://jquery.lm.msiservices.com and should be viewed with a WebKit based browser including but not limited to the following browser platforms:
- Safari or webkit nightly build
- Palm WebOS
The ingredients, HTML5 from the start
The doctype declaration of a JQuery Mobile application follows the HTML5 requirement:
Next comes the html head section:
<title>Linux Mag JQuery Mobile Sample</title>
The items to note here:
- Style sheet reference. JQuery Mobile relies heavily on CSS.
These includes are for the Alpha 2 release of JQuery Mobile.
When using libraries such as JQuery Mobile, these include files are used on virtually every page of your site or application. The terminolgy is getting blurred of late between what is a site or an application. I’ll leave that debate to others who are more interested in semantics.
The important aspect of the includes is that they need to be download from someone’s server. Broadly speaking there are two common practices:
- Option one is to download a copy of the needed include files and distribute them from your own server. The benefit of this approach is that your application won’t have unexpected changes occuring in the core library components. The downside is that you don’t get the latest features and bug fixes automatically. Additionally if you serve these files over and over from your own web server it may have a negative impact on your application’s performance and the bandwidth consumed may lead to increased hosting costs.
- The second option is to use a hosted version available from others such as Google or the JQuery project themselves. You can download the required files from JQuery’s Content Delivery Network (CDN). To learn more about this option visit the JQuery Mobile download page.
Now that we’ve got the required files into our application, let’s look at application structure.
JQuery Mobile relies heavily on the HTML5 data attribute. This attribute is somewhat of a magic bullet to HTML5. Any attribute which starts with “data-” is ignored by the rendering engine and is available for pushing data, instructions, etc. to an application. In the case of JQuery Mobile, the data attribute is used to help with application structure. Let’s look at an example.
The “data-role” attribute is used by JQuery Mobile to segment html elements into pages and parts of pages.
The image below shows the first page of the sample application.
First page of JQuery Mobile sample application
Here is the code which corresponds to the prior screen shot:
<div data-role="page" id="page1">
<h1>Header goes here</h1>
Content goes here
<a href="#page2" data-transition="slide">Slide to page two</a><br/>
Footer, Copyright, etc.
The page includes a header portion, a content portion, and a footer portion. Each of these is implemented as a div element and designated via the data-role attribute. Additionally the data-role tag is used to delineate a “page”. So, for starters we are interested in four values of the data-role attribute used by the div tag: