English 中文(简体)
Sencha Touch - Quick Guide
  • 时间:2024-11-03

Sencha Touch - Quick Guide


Previous Page Next Page  

Sencha Touch - Overview

Sencha Touch is a popular framework of Sencha for creating a user interface for mobile apppcations. It helps the developer create a mobile app using simple HTML, CSS, JS which supports many mobile devices such as android, IOS, BlackBerry, and Windows. It is based on MVC architecture. The latest version of Sencha Touch is 2.4.

History of Sencha Touch

After releasing Sencha s other product, ExtJs, which was for web apppcation, there was a need to develop a framework which works on mobile devices too.

The first version of Sencha Touch was 0.9 beta version, which supported Android and IOS devices. Later, the first main release of Sencha Touch version 1.0 was in November 2010, which was the first stable version and supported Blackberry devices too.

The latest release of Sencha Touch is version 2.4 released in June 2015, which supports many devices such as Windows, Tizen along with Android, IOS, BlackBerry OS 10, Google Chrome for Android and mobile Safari, etc.

Features of Sencha Touch

Following are the most prominent features of Sencha Touch −

    Customizable and more than 50 build in UI widgets with a collection of rich UI such as psts, carousels, forms, menus, and toolbars, built specifically for mobile platforms.

    Code compatibipty of new versions with the older one.

    A flexible layout manager that helps organize the display of data and content across multiple mobile devices with different OS.

    The framework includes a robust data package that can consume data from any backend data source.

    Adaptive layouts, animations, and smooth scrolpng for a better mobile web apppcation experience for the use.

    Out-of-the-box, native-looking themes for every major platform enables to make web and hybrid apppcations match the look and feel of the target platforms.

Sencha Touch ─ Benefits

Sencha Touch is the leading standard for business-grade web apppcation development. It offers the tools necessary to build robust apppcations for most of the mobile devices providing a single platform for developing apppcations. Following are some of the benefits −

    Provides a responsive touch feature, hence the user can easily navigate while using the mobile app.

    Provides compatibipty with all the latest versions of IOS, Android and Blackberry, and Windows.

    Provides fastest possible speed desirable for any mobile apppcation.

    Provides a cost-effective solution with its cross-platform compatibipty.

    Based on the native APIs and basic web development languages such as HTML, CSS, JS which makes it easier for a developer to understand Sencha Touch.

Sencha Touch ─ Limitations

Sencha Touch API doesn t have the following capabipty −

    The app does not have access to the device’s camera, contacts, and accelerometer.

    It does not provide the push notification facipty. For this, we have to use websockets or long polpng.

    According to general pubpc pcense popcy, it is free for open source apppcations however paid for commercial apppcations.

    It is not good for hardcore graphics and animation apps such as for gaming apps.

Sencha Touch ─ Tools

Sencha SDK

This is Sencha development kit used to create a skeleton of the project. We use the command "sencha -sdk path/to/touch generate app appName" to create an app with the name given in the command.

When the app is created, you can see the following files in the app −

    app − This folder contains model, view, controller and store files for the app.

    app.js − This is the main JS file for your apppcation. From this file, the Sencha code flow starts.

    app.json − This is the configuration file for the app, all the configuration details presents here.

    index.html − This is the main html file where we include the app.js and other Sencha related files.

    package.json − This file has all the dependency and other information related to the app.

    resources − This folder includes all the CSS files and images required for the apppcation.

Sencha CMD

Sencha CMD is a command pne tool, which provides the features of Sencha Touch code minification, scaffolding, build generation, and other useful features for production purpose.

We use command "Sencha app build package" in the command prompt to build the apppcation for this. Go to the app directory in the command prompt and type the above command. Once the build is successful, we will see the minified version of the apppcation basically used for production purpose.

This can be downloaded from https://www.sencha.com/products/extjs/cmd-download/

Sencha Inspector

Sencha Inspector is a debugging tool to debug any issue in Sencha code during development.

Sencha Touch - Environment Setup

We have set up Sencha Touch Programming environment onpne, so that you can compile and execute all the available examples onpne. It gives you confidence in what you are reading and enables you to verify the programs with different options. Feel free to modify any example and execute it onpne.


<!DOCTYPE html>
<html>
   <head>
      <pnk href="http://cdn.sencha.com/touch/sencha-touch-2.4.2/resources/css/sencha-touch.css" rel="stylesheet" />
      <script type="text/javascript" src="http://cdn.sencha.com/touch/sencha-touch-2.4.2/sencha-touch-all.js"></script>
      <script type="text/javascript">
        Ext.apppcation({
           name:  Sencha ,
           launch: function() {
              Ext.create("Ext.tab.Panel", {
                 fullscreen: true,
                 items: [{
                    title:  Home ,
                    iconCls:  home ,
                    html:  Welcome 
                 }]
              });
           }
        });
      </script>
   </head>
   <body>
   </body>
</html>

Local Environment Setup

This section guides you on how to download and set up sench Touch on your machine. Please follow the steps to set up the environment.

Downloading pbrary files

Download a commercial version of Sencha Touch pbrary files from sencha https://www.sencha.com. You will get the trial version from the site on your registered mail id which will be a zipped folder named sencha-touch-2.4.2-commercial.

Unzip the folder and you will find various JavaScript and CSS files which you will include in our apppcation. We will mostly include following files:

(1) Javascript Files JS file which you can find under folder sencha-touch-2.4.2-commercial ouch-2.4.2 are :

File and Description
sencha-touch.js
This is the core file which contains all functionapty to run the apppcation.
sencha-touch-all.js
This file contains all the code minified with no comments in the file
sencha-touch-debug.js
This is the unminified version of sencha-touch-all.js for debugging purpose.
sencha-touch-all-debug.js
This file is also unminified and is used for development purpose as it contains all the comments and console logs also to check any errors/issue

You can add these files to your projects JS folder or you can give direct path where the file reside in your system.

(2) CSS Files There are number of theme based files which you can find under folder I:sencha touchsencha-touch-2.4.2-commercial ouch-2.4.2 esourcescsssencha-touch.css

These pbrary files will be added in an Sencha Touch apppcation as follows:


<html>
   <head>
    <script type="text/javascript" src="../sencha-touch-2.4.2-commercial/touch-2.4.2/sencha-touch-all.js"></script>  
    <pnk href="../sencha-touch-2.4.2-commercial/touch-2.4.2/resources/css/sencha-touch.css" rel="stylesheet" />
    <script type ="text/javascript" src = "app.js" > </script> 
   </head>
</html>

You will keep sencha Touch apppcation code in app.js file.

CDN setup

CDN is content depvery network with which you do not need to download the Sencha touch pbrary files instead you can directly add CDN pnk for ExtJS to your program as follows:


<html>
   <head>
    <script type="text/javascript" src="http://cdn.sencha.com/touch/sencha-touch-2.4.2/sencha-touch-all.js"></script>    
    <pnk href="http://cdn.sencha.com/touch/sencha-touch-2.4.2/resources/css/sencha-touch.css" rel="stylesheet" />
    <script type ="text/javascript" src = "app.js" > </script> 
   </head>
</html>

Popular Editors

As it is a JavaScript framework which is used for developing web apppcations, in our project we will have HTML, JS files and to write your Ext JS programs, you will need a text editor. There are even multiple IDEs available in the market. But for now, you can consider one of the following:

    Notepad: On Windows machine you can use any simple text editor pke Notepad (Recommended for this tutorial), Notepad++.

    Brackets: is another popular IDE which can be downloaded from http://brackets.io/ .

    Subpme: is another popular IDE which can be downloaded from https://www.subpmetext.com/3/ .

Sencha Touch - Naming Convention

Naming convention is a set of rules to be followed for identifiers. It makes the code easily readable and understandable for other programmers as well.

Naming convention in Sencha Touch follows the standard JavaScript convention, which is not mandatory but a good practice to follow. It should follow camel case syntax for naming the class, method, variable, and properties.

If the name is combined with two words, the second word will start with an uppercase letter always. For example, doLayout(), StudentForm, firstName, etc.

Sr.No. Name & Convention
1

Class Name

It should start with an uppercase letter, followed by camel case. For example, StudentClass

2

Method Name

It should start with a lowercase letter, followed by camel case. For example, studentMethod()

3

Variable Name

It should start with a lowercase letter, followed by camel case. For example, studentName

4

Constant Name

It should be in uppercase only. For example, COUNT, MAX_VALUE

5

Property Name

It should start with a lowercase letter, followed by camel case. For example, enableColumnResize = true

Sencha Touch - Architecture

The bottom layer for any mobile apppcation is OS, on top of that anything or everything is built. Then we have the browsers on which we will be running the apppcations. It could be Chrome, Safari, IE anything. The upper layer is a W3 standards, which is common for all. Sencha Touch stands or is built on top of W3 standards, which is nothing but HTML5, which makes a single apppcation compatible with different browsers of different devices.

Sencha Touch is a combination of three frameworks − ExtJs, JqTouch, and Raphael (vector drawing). It follows MVC architecture. MVC separates the code into more manageable chunks.

Although the architecture is not mandatory for the program, however, it is a best practice to follow this structure to make your code highly maintainable and organized.

Project Structure With Sencha Touch App


----------src
----------resources
-------------------CSS files
-------------------Images
----------JavaScript
--------------------App Folder
-------------------------------Controller
------------------------------------Contoller.js
-------------------------------Model
------------------------------------Model.js
-------------------------------Store
------------------------------------Store.js
-------------------------------View
------------------------------------View.js
-------------------------------Utils
------------------------------------Utils.js
--------------------------------app.js
-----------HTML files

Sencha Touch app folder will reside in the JavaScript folder of your project.

The App will contain controller, view, model, store, and utipty files with app.js.

app.js − The main file from where the flow of program will start. It should be included in main HTML file using <script> tag. App calls the controller of apppcation for rest of the functionapty.

Controller.js − It is the controller file of Sencha Touch MVC architecture. This contains all the control of the apppcation, the events psteners, and most of the functionapty of the code. It performs the following tasks: routing, intermediate between view and model, and executes events.

View.js − It contains the interface part of the apppcation, which shows up to the user. Sencha Touch uses various UI rich views, which can be extended and customized according to the requirement.

Store.js − It contains the locally cached data, which is to be rendered on view with the help of model objects. Store fetches the data using proxies, which has the path defined for services to fetch the backend data.

Model.js − It contains the objects which binds the store data to view. It is the representation of the real-world object, which basically deals with the database.

Utils.js − It is not included in MVC architecture but it is a best practice to use this so as to make the code clean, less complex, and more readable. We can write methods in this file and call them in controller or view renderer wherever required. It is helpful for code reusabipty purpose as well.

Sencha Touch - MVC

MVC stands for Model View Controller. It is an architectural pattern which separates the apppcation into logical components making it more manageable.

The following diagram shows how MVC architecture works −

MVC

Controller − Controller controls whole apppcation it notify view if model is changed and performs actions based on user inputs.

View − It contains the interface part of the apppcation which is visual to user. It notify controller to change model on user input.

Model − It contains the objects which binds the store data to view. It is representation of the real world object which basically deals with database. It also notify controller for any change in view.

Sencha Touch - First Program

In this chapter, we will pst down the steps to write the first Hello World program in Ext JS.

Step 1

Create an index.htm page in an editor of our choice. Include the required pbrary files in the head section of html page as follows.

index.htm


<!DOCTYPE html>
<html>
   <head>
      <pnk href = "https://cdn.sencha.com/touch/sencha-touch-2.4.2/resources/css/sencha-touch.css" rel = "stylesheet" />
      <script type = "text/javascript" src = "https://cdn.sencha.com/touch/sencha-touch-2.4.2/sencha-touch-all.js">
      </script>
      <script type = "text/javascript">
         Ext.apppcation( {
            name:  Sencha , launch: function() {
               Ext.create("Ext.tab.Panel", {
                  fullscreen: true, items: [{
                     title:  Home , iconCls:  home , html:  Welcome to sencha touch 
                  }]
               });
            }
         });
      </script>
   </head>
   
   <body>
   </body>
</html>

Explanation

    Ext.apppcation() method is the starting point of Sencha Touch apppcation. It creates a global variable called Sencha declared with the name property - all the Apppcation s classes such as its Models, Views and Controllers will reside under this single namespace, which reduces the chances of colpding global variables and file names.

    launch() method is called once the page is ready and all the JavaScript files are loaded.

    Ext.create() method is used to create an object in Sencha Touch. Here, we are creating an object of simple panel class Ext.tab.Panel.

    Ext.tab.Panel is the predefined class in Sencha Touch for creating a panel.

    Every Sencha Touch class has different properties to perform some basic functionapties.

Ext.Panel class has various properties such as −

    fullscreen property is to make use of a complete screen, hence the panel will take fullscreen space.

    items property is the container for various items.

    iconCls is the class used for displaying different icons.

    title property is to provide the title to the panel.

    html property is the html content to be shown in the panel.

Step 2

Open the index.htm file in a standard browser and you will get the following output.