O'Reilly logo

jQuery Mobile by Jon Reid

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Initializing the Application

Now that we have the individual pages in place, we can start thinking about filling them in with data. It’s useful to wrap application initialization in a jQuery plug-in that initializes each page in a different method. Here’s the basic pattern, applied to jqmTweet:

(function($) {
  var methods = {
    initMainPage : function() {
    },

    initDetailPage : function() {
    },

    initSettingsPage : function() {
    },

    initAll : function() {
      $().initApp("initMainPage");
      $().initApp("initDetailPage");
      $().initApp("initSettingsPage");
    }
  }

  $.fn.initApp = function(method) {
    // Method calling logic
    if ( methods[method] ) {
      return methods[ method ].apply( this,
      Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.initAll.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist' );
    }
  }
})(jQuery);

Then on document ready we can just call the new jQuery.initApp() function and everything will be initialized.

Note

This is almost a straight up copy-and-paste of the jQuery plug-in pattern. I’ve left out the options since we don’t need them, and I’ve altered method calling logic so that initAll is the default method for the plug-in, but otherwise it’s the same. The jQuery plug-in pattern is very useful; if you don’t already know about it, you can read more on the jQuery documentation site at http://docs.jquery.com/Plugins/Authoring.

The initMainPage Method

We’ll start with the main page. The main page needs to ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required