Making An App With PhoneGap & jQm Part II: Loading JavaScript Properly 10

If you missed Part I: A Simple HTML5 Skeleton, you may want to go there and grab the code we wrote so far so that you can follow more easily what we'll do in this part.

One of the challenges in developing apps using PhoneGap and jQuery.Mobile is to make sure that the libraries are properly loaded before any critical code is executed. Another challenge is to make the difference between code which is due to execute only once, code which will execute on every activity, etc. In this part we'll explore those differences and I'll present to you a way of making sure that your functions execute at the right time.

Namespacing The Application's JavaScript

Namespacing your functions into an object is a great way of avoiding potential conflicts with existing functions; it also serves as an additional separation of your JS code from your HTML, it can make it easier to debug errors by keeping related entities close to each other. It may also make the use of a test framework like QUnit easier to implement.

Here is the base skeleton we'll use for our app:

var App = {
	"app_loaded": false,
	"testing_on_desktop": true,

	"init": function () {
	},

	"utilities": {
	},
};

A quick note before we go deeper: you can make use of the object notation in JavaScript to nest multiple namespaces, like I did with "utilities". This is a pretty cool feature to make your code a bit more organized than putting everything at the same level.

Setting Up jQuery.Mobile In <head>

As we've seen in Part 1, jQuery.Mobile is a bit special in the sense that it modifies the DOM upon loading. For this reason, any configuration code we'd like to see executed must be set before the library is loaded, using the "mobileinit" event which marks the beginning of jQuery.Mobile's execution.

As an example of what you can configure, as my app needs to "phone home" I had to activate the cors option in both jQuery and jQuery.Mobile. The full list of configuration variables is available here. Here's the result in <head>:

	<script charset="utf-8" src="js/app.js"></script>
	<script>
		jQuery(document).bind("mobileinit", function () {
			console.log("configuring jqm...");

			// required for "phoning home" (load external assets, etc.)
			jQuery.support.cors = true;
			jQuery.mobile.allowCrossDomainPages = true;
			
			jQuery.mobile.phonegapNavigationEnabled = true;
			jQuery.mobile.defaultDialogTransition = "pop";
			jQuery.mobile.defaultPageTransition = "none";
			
			jQuery.mobile.loader.prototype.options.text = "loading";
			jQuery.mobile.loader.prototype.options.textVisible = true;
			jQuery.mobile.loader.prototype.options.theme = "a";
		});
		App.init();
	</script>
	<script charset="utf-8" src="js/jquery.mobile-1.3.1.min.js"></script>

I advise you to systematically add console.log("[funcName]") at the beginning of every function as it makes debugging much much easier, especially when callbacks are involved because they can be rather messy in the way/order they fire. Oh, notice the App.init() call? Let's see what we can put in there.

Making An App With PhoneGap & jQm Part I: A Simple HTML5 Skeleton 5

In this series, I will explain how I am using PhoneGap & jQuery.Mobile (on top of jQuery) to build an App for Android while being able to test it in my desktop browser. This is not an introduction on PhoneGap or jQuery.Mobile per se. I will not focus on Java and I won't explain how to install PhoneGap or build the app from an IDE like Eclipse; you may need to read some other tutorial first to cover those points and then come back to this page when you can build your app from your IDE. Don't worry too much though, you won't need much knowledge of Java for I will expose some ways we can use to test the app in a desktop browser without the need to recompile it everytime we change something.

Now this is my first app, which means that most of the code I've put in so far required some trial-and-error for it to work. Hopefully you'll find (parts of) this tutorial useful and you'll be able to code your way faster than me!

Note: I wouldn't dare pretending my code is perfect, and for the very same reason it's my first app there are most certainly better ways of doing some things; if you have any question or remark concerning my code, please do not hesitate to leave a comment, that's the best way for everyone to improve their skills.

Table of Contents

Here is how the tutorial is split:

Part I: A Simple HTML5 Skeleton
HTML5 basic structure, resources order, multiple pages and a menu with icons
Part II: Loading JavaScript The Right Way
JavaScript load order, jqm settings, App.init() & execution levels
Part III: A Stateless Authentication Layer
REST concepts, public/private tokens, some PHP server code
Part IV: ?
I'm not there yet!

Let's Start With a (Very) Basic HTML5 Template