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

Dynamically loading Ext JS classes


Ext JS 4 gives us the ability to only load the parts of the framework we need, as and when we need them. In this recipe, we will explore how to use the framework to automatically load all our class dependencies on the fly.

How to do it...

We are going to use the Vehicle and Plane classes that we created in the Using inheritance recipe earlier to demonstrate dynamic loading.

  1. Configure the Ext.Loader class to enable it and map our namespaces to a physical path. This should be added before your Ext.onReady call:

    Ext.Loader.setConfig({
        enabled: true,
        paths: {
            'Cookbook': 'src/Cookbook'
        }
    });
  2. Create individual files in the src/Cookbook folder for the Vehicle and Plane classes, naming each the same as the class name (excluding the namespace).

  3. Call the Ext.require method, inside our Ext.onReady function, passing in the class we need, and a callback function, which is executed after the class and all its dependencies have loaded, containing our code:

    Ext.require('Cookbook.Vehicle', function(){
        var van = Ext.create('Cookbook.Vehicle', 'Ford', 'Transit', 60);
        van.travel(200);
    });
  4. Execute the code and monitor your Developer Tools console and HTML tabs and you will see the Ext.define's callback being displayed and the new script tag being injected into the HTML:

How it works...

The initial configuration of the Ext.Loader class is vital for our classes to be loaded correctly, as it defines how class names are mapped to file locations so the Loader class knows where to find each class it is required to load. It also highlights the need for strict naming conventions when it comes to creating your files.

In our example, the paths configuration tells the Loader that any required classes within the Cookbook namespace should be loaded from the src/Cookbook directory.

We then call the Ext.require method, (an alias of the Ext.Loader.require method) which takes the class name specified and resolves its URL based on the paths configuration, and if it hasn't already been loaded previously, injects a script tag into the HTML page to load it. Once this load has happened the specified callback function is executed where you can create instances of the class with the knowledge that it has been fully loaded.

The Ext.require method accepts either a single or array of string values that will all be loaded prior to the callback being executed.

There's more...

One of the great things about the Ext.Loader class is that it is recursive and won't stop until all the files needed by the original required classes are loaded. This means that it will load all classes referenced in the extend, mixins, and requires configuration objects.

We will demonstrate this by creating an instance of Cookbook.Plane , which extends the Cookbook.Vehicle class. If we execute the following code, and monitor your developer tool as we did before, we will see both classes being loaded and created:

Ext.require('Cookbook.Plane', function(){
   var plane = new Ext.create('Cookbook.Plane', 'Boeing', '747', 500, 35000);
   plane.travel(200);
});

See also

  • See the very first recipe covering the details of how to define and work with classes.

  • A fixed folder structure is required for dynamic loading. See the recipe Creating your application's folder structure in Appendix, Ext JS 4 Cookbook-Exploring Further for a detailed explanation.