Book Image

Ext JS 4 Web Application Development Cookbook

By : Andrew Duncan, Stuart Ashworth
Book Image

Ext JS 4 Web Application Development Cookbook

By: Andrew Duncan, Stuart Ashworth

Overview of this book

<p>Ext JS 4 is Sencha’s latest JavaScript framework for developing cross-platform web applications. Built upon web standards, Ext JS provides a comprehensive library of user interface widgets and data manipulation classes to turbo-charge your application’s development. Ext JS 4 builds on Ext JS 3, introducing a number of new widgets and features including the popular MVC architecture, easily customisable themes and plugin-free charting. <br /><br /><em>Ext JS 4 Web Application Development Cookbook</em> works through the framework from the fundamentals to advanced features and application design. More than 130 detailed and practical recipes demonstrate all of the key widgets and features the framework has to offer. With this book, and the Ext JS framework, learn how to develop truly interactive and responsive web applications.<br /><br />Starting with the framework fundamentals, you will work through all of the widgets and features the framework has to offer, finishing with extensive coverage of application design and code structure.<br /><br />Over 110 practical and detailed recipes describe how to create and work with forms, grids, data views, and charts. You will also learn about the best practices for structuring and designing your application and how to deal with storing and manipulating data. The cookbook structure is such that you may read the recipes in any order.<br /><br />The <em>Ext JS 4 Web Application Development Cookbook</em> will provide you with the knowledge to create interactive and responsive web applications, using real life examples.</p>
Table of Contents (19 chapters)
Ext JS 4 Web Application Development Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Extending Ext JS components


It is regarded as best practice to create each of your components as extensions of Ext JS' own components and store them in separate files. This approach aids code reuse, helps organize your code and makes maintenance a much easier task. In this recipe, we will discuss how to go about extending an Ext JS component to create a pre-configured class and then configuring it to make our own custom component.

How to do it...

We will define an extension of the Ext.panel.Panel class to create a simple display panel.

  1. Define a new class under the Cookbook namespace, which extends the Ext.panel.Panel class:

    Ext.define('Cookbook.DisplayPanel', {
        extend: 'Ext.panel.Panel'
    });
  2. Override the Ext.panel.Panel's initComponent method and call the parent class' initComponent method:

    Ext.define('Cookbook.DisplayPanel', {
        extend: 'Ext.panel.Panel',
    
        initComponent: function(){
            // call the extended class' initComponent method
            this.callParent(arguments);
        }
    });
  3. Add our own component configuration to the initComponent method by applying it to the class itself:

    initComponent: function(){
        // apply our configuration to the class
        Ext.apply(this, {
            title: 'Display Panel',
            html: 'Display some information here!',
            width: 200,
            height: 200,
            renderTo: Ext.getBody()
        });
        
        // call the extended class' initComponent method
        this.callParent(arguments);
    }
  4. Create an instance of our preconfigured class and show it:

    var displayPanel = Ext.create('Cookbook.DisplayPanel');
    displayPanel.show();

How it works...

Our first step creates our new class definition and tells the framework to give our new class all the functionality that the Ext.panel.Panel has, through the use of the extend config option.

We then introduce an override for the initComponent method, which is used by each component to add its own configuration and perform any actions that are needed to set the component up. In order to ensure that this component behaves as it should, we call the parent class' initComponent method (in this case, Ext.panel.Panel) using the callParent method.

Next, we give our new class the configuration we want. We do this by using the Ext.apply method, which merges our configuration object into the class itself.

We are now able to instantiate our new class using its defined name and it will automatically be configured with all the properties we applied in the initComponent method. This means we can create a DisplayPanel anywhere in our code and only have to define it once.

There's more...

We can take this idea further by integrating our own functionality into an extended component by overriding its functions. We are going to create a custom TextField that includes some information text below the field to help the user complete the form field correctly:

  1. First we create our basic structure for extending the Ext.form.field.Text component:

    Ext.define('Cookbook.InfoTextField', {
    
        extend: 'Ext.form.field.Text'
        
    });
  2. Next, we override the onRender function, which is used to render the component to the page. In our override, we immediately call the parent's onRender method, so the field is fully rendered before our code is executed. We then use the Ext.core.DomHelper class to insert a new div element, after the textfield, containing the value from the component's infoText property:

    Ext.define('Cookbook.InfoTextField', {
        extend: 'Ext.form.field.Text',
        onRender: function(){
            this.callParent(arguments);
    
            // insert our Info Text element
            Ext.core.DomHelper.append(this.getEl(), '<div>' + this.infoText + '</div>');
        }
    }, function(){
        console.log('Cookbook.InfoTextField defined!');
    });
  3. We can now create our new InfoTextField class wherever we like and display any value that we would like using the infoText config option, like this:

    var infoTextField = Ext.create('Cookbook.InfoTextField', {
        renderTo: Ext.getBody(),
        fieldLabel: 'Username',
        infoText: 'Your Username must be at least 6 characters long.'
    });
    
    infoTextField.show();

See also

  • Creating custom classes with the new Ext JS class system for an explanation on creating classes and their structure.

  • We extend classes throughout this book, however, if you would like to see it in action we suggest you take a look at Modeling a data object, in Chapter 7, Working with the Ext JS Data Package.

  • The next recipe covers overriding in more detail.