OpenSource For You

Three Frameworks You Can Use to Develop a Hybrid Mobile App

Creating a mobile app isn't as difficult or as complicate­d as it sounds. The tools are all available on the Internet. With a little bit of the right guidance, your dream mobile app can be yours to share with the world. In this article, the author explains

-

L et’s make a hybrid mobile app using Angular JS, jQuery Mobile and PhoneGap. Before starting with the code, I would like to explain a little about what a hybrid mobile app is, and what Angular JS, jQuery Mobile and PhoneGap are.

Mobile apps are generally of three major types—native apps, mobile Web apps and hybrid mobile apps. Native apps are made using the platform-specific SDK, tools and languages provided by the platform vendor. So, for instance, you can make native Android apps using Java with the Android SDK. Mobile Web apps, on the other hand, are websites or Web applicatio­ns optimised for mobile browsers. These are developed using Web developmen­t technologi­es - HTML, CSS and JavaScript, but they do not have the luxury of accessing the native features of the mobile. Hybrid mobile apps use the best of both the worlds, and are made using Web developmen­t technologi­es like HTML, CSS and JavaScript, but they run inside a native container utilising the device’s browser engine to render the HTML and process JavaScript. Beyond this, there are abstractio­n layers available that enable developers to access the native features of the device as well.

jQuery Mobile is a very famous touch-optimised Web/ mobile framework built on top of the jQuery core, by the jQuery team. It has a very minimal learning curve. It is robust enough and the views created using jQuery Mobile adjust themselves automatica­lly based on the browser’s form factor. In other words, there is a very minimalist­ic CSS change that the developer needs to do when it comes to designing views for various form factors using jQuery Mobile.

Angular JS is a MVW framework that focuses on extending traditiona­l HTML to cater to dynamic content. MVW stands for Model-ViewWhatev­er, where ‘whatever’ implies “whatever works for you”, though some people also like to bucket Angular JS as a MVC or a MV* framework. Angular JS stands out from other frameworks because it eliminates DOM manipulati­on and ensures automatic synchronis­ation between models and views.

PhoneGap is an open source framework that helps in building hybrid mobile apps (cross-platform apps). It helps to wrap the HTML/CSS/JavaScript code inside the native container for the respective target platform, and also provides an abstractio­n layer in the form of a JavaScript library to access native device features.

Having discussed each of the technologi­es to be used in building our app, let us now jump into some coding. Let’s develop a single-view app where the user will be able to fetch a list of all the car companies from around the world, then select one company from the list, and find out all the models that belong to that company.

The app structure will be as follows. At the root level,

there will be an index.html file, a config.xml file and the icon. png file. Then there will be the respective folders for the library files (containing the angular.js file), the jquery-mobile files, the images to be used, the custom JavaScript code, and the custom styling, if needed.

Let us first look into the relevant code snippet from the index.html file:

<div data-role="page"> <div data-role="header"> <h1>Cars</h1> </div> <div data-role="content" id="main" ng-app="carsApp" ngcontroll­er="myAppContr­oller"> <a data-role="button" ng-click="fetchCarMa­kes()">Get Makes</ a> <ul data-role="listview" data-inset="true" id="dataListVi­ew"> <li ng-repeat="data in listViewDa­ta"> <a ng-click="fetchModel­s(data)"> <imgng-src="images/{{data.countryFla­g}}" class="ui-li-icon">

{{data.name}} </a> </li> </ul> </div>

</div>

Here, we are creating a page using jQuery Mobile. To convert any div tag into a page in jQuery Mobile, we just need to add data-role=“page” in the div tag. The page has two major sets of content—the page header and the page content—each declared using its respective data-role. The content of the page has one button, which when clicked by the user can get the names of all the car companies from around the world. This data is displayed in the listview below the button. The content div uses two AngularJS declaratio­ns— ng-app and ng-controller. The first one allows the developer to declare a root element for the applicatio­n whose behaviour can be modified. ng-controller lets the developer specify a JavaScript controller class that can be used to evaluate HTML expression­s. The li element is the listview using the ng-repeat declaratio­n. ng-repeat helps the user to instantiat­e elements by using items from a collection. Here, the collection is the listViewDa­ta, which will be declared inside the controller class— myAppContr­oller. The image source can use ng-src. All the data that needs to be accessed in the HTML can be accessed using {{}}.

Next, let us look at the relevant snippet from the JavaScript: angular.module('carsApp', []) .controller('myAppContr­oller', ['$scope', '$http', function($scope,$http) { $scope.listViewDa­ta = []; $scope.fetchCarMa­kes = function() { $.mobile.loading( "show" ); $scope.listViewDa­ta = []; $http({method: 'GET', url: getCarMake­sURL}). success(function(data, status, headers, config) { angular.forEach(data,function(value,obj){

angular.forEach(value,function(makesDetai­ls,ind

ex){ switch (makesDetai­ls.make_country) { case "Italy": $scope.listViewDa­ta.push({name:makesDetai­ls. make_display,countryFla­g: "italy.png",id:makesDetai­ls.make_ id});

ex){

}])

}); //.. //.. //.. } break; //.. //.. //.. $("#dataListVi­ew"). listview("refresh"); //.. //.. } $scope.fetchModel­s = function(makeName){ //.. $http({method: 'GET', url:modelMakeU­RL}). success(function(data, status, headers, config) { angular.forEach(data,function(value,obj){

angular.forEach(value,function(modelDetai­ls,ind

$scope.listViewDa­ta.push({name:modelDetai­ls. model_name,countryFla­g: makeName.countryFla­g,id: modelDetai­ls.model_make_id});

});

Here, for ng-app=“carsApp” declared in the HTML, we declare an angular module, which contains our controller. Inside the controller, we handle two events—the button click and the listview item click.

There are two major objects used in the controller - $scope and $http. The $scope object refers to the applicatio­n model. Scope is the glue between the applicatio­n controller and the view. Hence, using Scope, we can directly make changes in the view with our controller. The $http object helps for communicat­ion with remote HTTP servers.

In the first click event - fetchCarMa­kes - we first show the jQuery Mobile loader using $.mobile.loading(“show”). We then make a ‘GET’ call to fetch all the car makers from across the world. When we receive the JSON response, we parse it accordingl­y and push it into the listViewDa­ta array. Once the array is completely populated, the ul on the view will automatica­lly get updated. But to update the jQuery Mobile listview, we need to explicitly call $("#dataListVi­ew").listview("refresh") on the listview.

Similarly, for the li click event— fetchModel­s— we pass the car maker argument into the function and use it to make the appropriat­e ‘GET’ call. And, just as above, when we receive the JSON response, we parse it accordingl­y and push it into the listViewDa­ta array. As a result, the same ul on the view gets updated automatica­lly.

We can first test this locally using the Ripple emulator on Google Chrome. When the initial screen is run, we can see in Figure 1 that only the header and the ‘Get Makes’ button is shown. When we click on the button, we see the jQuery Mobile loader, and then automatica­lly, listview appears with the car makers’ names and their country flags, as seen in Figure 2. This list is very long and is scrollable, automatica­lly. Next, when we click on any particular car maker, the loader appears again, and the listview is updated with the car models as seen in Figure 3.

So now when things are working properly, we can package everything to run it on a device. All we need to do is to zip the entire folder and open www.build.phonegap. com. We can upload the zip file there, and get the respective installati­on files for various platforms.

The codebase can be found at https://github.com/ kazekagega­ara/AngularJSC­arApp. You can get the entire codebase, put it into a zip file, build it as instructed above and use the app on your own devices.

 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ?? Figure 2: Listview populated with car maker names
Figure 2: Listview populated with car maker names
 ??  ?? Figure 3: Listview refreshed with car model names for a particular car make
Figure 3: Listview refreshed with car model names for a particular car make
 ??  ?? Figure 1: First screen of the app
Figure 1: First screen of the app

Newspapers in English

Newspapers from India