Book Image

HTML5 iPhone Web Application Development

By : Alvin Crespo
Book Image

HTML5 iPhone Web Application Development

By: Alvin Crespo

Overview of this book

<p>Create compelling web applications specifically tailored for distribution on iOS Safari. Work through real world examples with references, and in-depth discussions on the approach; including its benefits and drawbacks.<br /><br />"HTML5 iPhone Web Application Development" strives to teach all levels of developers, beginners and professionals, the process of creating web applications for iOS Safari. Utilizing current industry standards for frontend development, learn to take advantage of HTML5, CSS3 and JavaScript to create compelling software.<br /><br />Start with reviewing current industry standards for frontend development, and end with creating a native application using the same codebase.</p> <p>Your journey will begin with an overview of current industry standards for frontend technology, quickly moving to solve real world issues; from creating a resizable or responsive gallery, to creating a single page application that utilizes the popular Backbone.js framework.</p> <p>"HTML5 iPhone Web Application Development" aims to make you an expert in developing web applications for the iOS Safari platform.</p>
Table of Contents (17 chapters)
HTML5 iPhone Web Application Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Integrating a custom module template


Now, to get the rest of our architecture together we need to open up every other App file in the JavaScript directory we are in (/js/App).

When we have these files open, we need to paste the following template, which is based on the script we've written for App.js:

var App = window.App || {};

App.Module = (function(window, document, $){
    'use strict';
    
    var _defaults = {
        'name': 'Module'
    };

    function Module(options) {
        this.options = $.extend({}, _defaults, options);

        this.$element = $(this.options.element);
    }

    Module.prototype.getDefaults = function() {
        return _defaults;
    };

    Module.prototype.toString = function() {
        return '[ ' + (this.options.name || 'Module') + ' ]';
    };

    Module.prototype.init = function() {

        return this;
    };

    return Module;

}(window, document, Zepto));

When we have each template in, we must then change Module to the appropriate type, that is Video, Audio, Location, and so on.

Once you are done with pasting in the section and changing the names, you should be all set with the basic JavaScript architecture.

Including our scripts

One of the last items you will need to take care of is including this basic architecture into each index.html file. In order to do this, you will need to paste the following code at the bottom of the page, right after the inclusion of helper.js:

<script src="js/App/App.js"></script>
<script src="js/App/App.Audio.js"></script>
<script src="js/App/App.Forms.js"></script>
<script src="js/App/App.Location.js"></script>
<script src="js/App/App.SinglePage.js"></script>
<script src="js/App/App.Touch.js"></script>
<script src="js/App/App.Video.js"></script>
<script src="js/main.js"></script>

We are basically including each script of the framework. What's important here is to always include App.js first. The reason for this is that App.js creates the App object and directly modifies it. If you include it after all the other scripts, then App.js will overwrite the other scripts because it's directly affecting the App object.

Initializing our framework

The last item we need to take care of is main.js, which includes the initialization of our application. We do this by wrapping our code in IIFE and then exposing the instance to the window object. We do this with the following code:

(function(window, document) {
    'use strict';
    
    var app = new App({
        'element': document.querySelector('.site-wrapper')
    });

    window.app = app;

}(window, document));

What we've seen earlier is an IIFE being assigned to an object. Here we don't see that because it's not necessary. We just want to make sure our code would not affect the rest of the code, which in most cases would not happen because of the simplicity of this project. However, as a best practice I try to self contain my code in most cases.

The difference in the previous code is that we see the initialization of our framework here:

var app = new App({
    'element': document.querySelector('.site-wrapper')
});

We do that by using the new keyword, creating a new instance of App, and then passing it an object, which will be merged into our default options we previously wrote.

Note

querySelector is a JavaScript method that is attached to the document object. This method accepts a selector that we would normally use in CSS, parse DOM, and find the appropriate element. In this case, we are telling our application to self contain itself to the element with the site-wrapper class.

When we finally initialize our application, we then attach app to the window object:

window.app = app;

This basically makes it accessible anywhere in our application by attaching it to the window object.

We are now done with the framework for our application. Although we don't have anything being manipulated on the page, or have attached any events that correlate with a user's input, we now have a solid foundation for coding that follows best practices, is effective, efficient, and easily accessible.