Book Image

Mastering Ext JS

By : Loiane Groner
Book Image

Mastering Ext JS

By: Loiane Groner

Overview of this book

<p>Ext JS 4 is a JavaScript framework that provides you with the resources to build multi-browser, high-performance, and rich Internet applications.<br /><br />Mastering Ext JS is a practical, hands-on guide that will teach you how to develop a complete application with Ext JS. You’ll begin by learning how to create the project’s structure and login screen before mastering advanced level features such as dynamic menus and master-detail grids, before finally preparing the application for production.<br /><br />Mastering Ext JS will help you to utilize Ext JS to its full potential and will show you how to create a complete Ext JS application from the scratch, as well as explaining how to create a Wordpress theme.</p> <p><br />You will learn how to create user and group security, master-detail grids and forms, charts, trees, and how to export data to excel including PDF and images, always focusing on best practices.</p> <p><br />You will also learn how to customize themes and how to prepare the application to be ready for deployment upon completion. Each chapter of the book is focused on one task and helps you understand and master an individual aspect of the application.</p> <p><br />By the end of the book, you will have learned everything you need to know to truly master Ext JS and to start building advanced applications.</p>
Table of Contents (20 chapters)
Mastering Ext JS
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
Index

Creating the structure of the application using MVC


Let's get started and get our hands on the code. The first thing we are going to do is to create the application using the MVC structure. Sencha Command (Sencha Cmd) provides us the capability of creating the application automatically. And creating the application with Sencha Cmd is helpful because it already creates the structure of the application according to the MVC architecture, and it also provides all the files we need to build the application for production and customizing the theme (we will learn how to do it later on this book).

A quick word about MVC

MVC stands for Model-View-Controller. It is a software architecture pattern that separates the representation of the information from the user's interaction with it. The Model represents the application data, the View represents the output of the representation of the data (Form, Grid, Chart), and the Controller mediates the input converting it to commands for the Model or View.

Ext JS uses MVCS, which is a Model-View-Controller-Store pattern. The Model is a representation of the data we want to manipulate in our application, a representation of a table from the database. The Views are all the components and screens we create to manage the information of a Model. As Ext JS is event-driven, all the Views fire events when the user interacts with them, and the Controller will capture these events and will do something, redirecting the command to the Model (or Store) or the View. The Store in Ext JS is very similar to the DAO (Data Access Object) pattern used on the server side.

For a quick example, let's say we have a WidgetA which is a Grid panel that displays all the records from a table A. This table is represented by ModelA. StoreA is responsible for retrieving the information (collection of ModelA from the server). When the user clicks on a record from WidgetA, a window will be opened (called WidgetB) displaying information from a table B (represented by ModelB). And of course, StoreB will be responsible for retrieving the collection of ModelB from the server. In this case, we will have ControllerA to capture the click event from WidgetA and do all the required logic to display WidgetB and load all the ModelB information. If we try to put this in a quick reference diagram, it would be something like the following diagram:

Creating the application

We are going to create the application inside the htdocs folder of the xampp directory and our application will be named masteringExtjs.

Before we start, let's take a look how the htdocs folder looks like.

We have the Xampp files inside it and the Ext JS 4.2 folder.

The next step is to use the Sencha Cmd to create the application for us. To do so, we need to open the terminal application that comes with the operating system we use. For Linux and Mac OS users, it is the terminal application, and for Windows users, the command prompt application.

Here are the steps we are going to execute: first we need to change the current directory to Ext JS directory (htdocs/ext-4.2.0.663 directory in this case), and then we will use the following command:

sencha generate app Packt ../masteringextjs

The sencha generate app command will create the masteringextjs directory inside the htdocs folder with necessary file structure required by the MVC architecture. Packt is the name of the namespace of our application, meaning that every class we create is going to start with Packt, as for example: Packt.model.Actor, Pack.view.Login, and so on. And the last argument passed to the command is the directory where the application will be created. In this case it is inside a folder named masteringextjs, which is located inside the htdocs folder.

After the command finishes its execution, we will have something like the following:

But why do we need to create a project structure like this one? This is the structure used by Ext JS MVC applications.

Note

For more information about the sencha generate app command, please consult: http://docs.sencha.com/extjs/4.2.0/#!/guide/command_app.

Let's see what each folder does.

First, we have the app folder. This is where we will create all the code for our application. Inside the app folder we can find the following folders as well: controller, model, store, and view. We can also find the app.js file. Let's talk about each one:

  • In the model folder we will create all files that represent a Model, which is an Ext JS class that represents a set of fields, meaning an Object that our application manages (Actor, Country, Film). It is similar to a class on the server side with only the attributes of the class plus getter and setter methods used to represent a table from the database.

  • In the store folder we will create all the store classes, which is a cache of a collection of Models. They are very similar to DAO (Data Access Object); classes used on server-side languages to perform CRUD operations on the database. And as Ext JS does not communicate directly with databases, the store classes are used to communicate with the server side or a local storage used with a proxy (Proxies are used by Stores to handle the loading and saving of the Model data).

  • In the view folder we will create all view classes, also known as the UI Components (User Interface Components), such as the Grid panel, the Tree panel, the Menu, the Form panel, the Window, and so on. In these classes we will only code what the user will see on the screen; we will not handle the events fired by a component (the Grid panel, the Tree panel, the Menu, the Form panel, the Window are subclasses of the Component class).

  • And finally, in the controller folder we will handle all the events fired by the components (events fired because of the life cycle of the component or because of some interaction of the user with a component). We always need to remember that Ext JS is event driven, and on the Controller classes we will control these events and update any Model, View, or Store (if needed).

We also have the app.js file. This is the entry point of the application. We will talk more about it in a few paragraphs.

Then, going back to the masteringextjs directory, we have a few more files and directories:

  • app.json: This is a configuration file used by Sencha Cmd. If we open it, we will see only a JSON object with the name of the application (Packt).

  • bootstrap.css and bootstrap.js: Both files are also created by Sencha Cmd and should not be edited. The CSS file contains the import of the theme used by the application (which is the blue classic theme) and the JS file contains some require directives, the custom xtype attributes and other meta-driven class system features.

  • build.xml: Sencha Cmd uses Apache Ant (http://ant.apache.org/), which is a Java tool used to build Java projects. Ant uses a configuration file named build.xml, which contains all the required configurations and commands to build a project. Sencha Cmd uses Ant as engine to build an Ext JS application in the background (while we simply need to use a command). This is the reason why we need to have the Java SDK installed to use some of the Sencha Cmd features.

  • index.html: This is the index file of our project. This is the file that will be rendered by the browser when we execute our application. Inside this file we will find the import of the bootstrap CSS and JS file, along with the import of the Ext JS framework file (ext/ext-dev.js and the app/app.js file).

  • ext: Inside this folder we can find all the Ext JS framework files (ext-all, ext-all-debug, ext-dev) and also its source code.

  • overrides: When we create the application it is empty. But inside this folder we should create any Ext JS overrides that we need to create for our project.

  • packages: Inside this folder we can find all the packages managed by Sencha Cmd. A theme is a package. For more information about packages, please go to http://docs.sencha.com/extjs/4.2.0/#!/guide/command_packages.

  • resources: Inside this folder, we will place all the CSS files we need to create for our application (with custom styles, CSS for icon images, and so on), and also all the static files (images).

  • sass: Inside this folder we can find some Sass files used to create themes.

Let's start with the hands-on now!

First, we need to edit the app.js file. This is how the file looks like:

Ext.application({
    name: 'Packt',

    views: [
        'Main',
        'Viewport'
    ],

    controllers: [
        'Main'
    ],

    autoCreateViewport: true
});

We are going to change it so that it looks like the following:

Ext.application({ // #1

    name: 'Packt', // #2

    launch: function() { // #3
         console.log('launch');   // #4
    }
});

On first line of the previous code we have the Ext.application declaration (#1). This means that our application will have a single page and the parent container of the app will be the Viewport. The Viewport is a specialized container representing the viewable application area that is rendered inside the body tag of the HTML page (<body></body>). It also manages the application's size inside the browser and manages the window resizing.

Inside Ext.application we can also declare models, views, stores, and controllers used by the application. We will add this information to this file as we create new classes for our project.

We need to declare the name of the application which will be the namespace (#2).

We can also create a launch function inside Ext.application (#3). This function will be called after all the application's controllers are initialized and this means that the application is completely loaded. So this function is a good place to instantiate our main view. For now, we will only add console.log (#4), which just prints on the browser's JavaScript interpreter console to verify if the application was loaded successfully.

Note

Do we need to use Ext.onReady when using Ext.application?

The answer is no. We only need to use one of the options. According to the Ext JS API documentation, Ext.application loads the Ext.app.Application class and starts it up with the given configuration after the page is ready and Ext.onReady adds a new listener to be executed when all required scripts are fully loaded. And if we take a look at the source code for Ext.application we have:

Ext.application = function(config) {
    Ext.require('Ext.app.Application');
    Ext.onReady(function() {
        new Ext.app.Application(config);
    });
};

This means that Ext.application is already calling Ext.onReady, so we do not need to do it twice. So use Ext.onReady when you have a few components to be displayed and they are not in the MVC architecture (similar to the jQuery $(document).ready() function) and use Ext.application when you are developing an Ext JS MVC application.

To execute this application on the browser, we can access http://localhost/masteringextjs, and we will get the following output:

Now we need to start building the application.