Shortlink

PhoneGap name confusion

If you have been working with PhoneGap 1.0 and trying to move to the latest version of PhoneGap, 1.5, one of the things that may leave you scratching your head, is a set of new files “cordova-1.5.0.js” and “cordova-1.5.0.jar” found in the PhoneGap distribution folder, instead of phonegap.js and phonegap.jar files.
You will notice that the MDS AppLaud plugin in Eclipse is also pretty confused about it.

This blog post may clear you off the name confusion.

Shortlink

Loading PhoneGap library for different devices

Maintaining single codebase while building native mobile applications is one of the reasons for using PhoneGap. PhoneGap provides a separate JavaScript library for different devices. Based on the device you have to load the appropriate PhoneGap JS files.
Say, your application is targeting the Android, iPhone and Blackberry platforms, you can have the PhoneGap JS file (phonegap-versionNumber.js in the earlier versions and cordova-versionNumber.js in the current version) provided for each of these platforms renamed to
phoneGap-android.js, phoneGap-iphone.js, phonegap-blackberry.js (or)
cordova-android.js, cordova-iphone.js, cordova-blackberry.js

Based on the device that is accessing the application one of these three files can be loaded like shown below.

var userAgent = navigator.userAgent.toLowerCase();

if (userAgent.match(/android/)) {
        document.write("<script src='phonegap-android.js'></script>");
    } 
    else if (userAgent.match(/iphone/)){
        document.write("<script src='phonegap-iphone.js'></script>");
    }
    else if (userAgent.match(/blackberry/)){{
        document.write("<script src='phonegap-blackberry.js'></script>"); 
    }
Shortlink

Video lectures on Sencha Touch 2.0

You can view few introductory video sessions on Sencha Touch 2.0. You can find the videos in DuraSoft site as well.

The plan is to upload atleast 2 videos per week.

Shortlink

Update on video sessions in Sencha Touch 2.0

My work on creating video sessions in Sencha Touch 2.0 is progressing at a good speed. Thanks to the increasing number of inquiries regarding this, which is keeping me on my toes. Looks like it’ll take couple of more days before uploading a sizeable number of videos in this topic.

Shortlink

Global variables in JavaScript

It’s normal to get caught in the web of global variables, while working with JavaScript. Global variables tend to make the code less readable and maintainable. Douglas Crockford suggests a pattern that can be used to maintain global variables in our code.

Let’s say you have the following code.

var userName = "";
var password = "";
var userType = "";

function loadCredentialsFromLocalStorage(){
 userName = localStorage.getItem("userName");
 password = localStorage.getItem("password");
 userType = localStorage.getItem("userType");
}

The global variables userName, password and userType variables can be written as:

var AGLOBALNAMESPACENAME = {}; 
AGLOBALNAMESPACENAME.user = {
               userName : "", 
               password : "", 
               userType : ""
           }; 

function loadCredentialsFromLocalStorage(){
 AGLOBALNAMESPACENAME.user.userName = localStorage.getItem("userName");
 AGLOBALNAMESPACENAME.user.password = localStorage.getItem("password");
 AGLOBALNAMESPACENAME.user.userType = localStorage.getItem("userType");
}

In the code above, you can create a variable giving it a Global namespace name and have all the ‘actual’ global variables assigned to it. The declaration of the global variables can be moved to a separate file as well. Hence the code becomes lot more readable and easy to maintain.