Book Image

Ext JS 3.0 Cookbook

Book Image

Ext JS 3.0 Cookbook

Overview of this book

Using Ext JS you can easily build desktop-style interfaces in your web applications. Over 400,000 developers are working smarter with Ext JS and yet most of them fail to exercise all of the features that this powerful JavaScript library has to offer. Get to grips with all of the features that you would expect with this quick and easy-to-follow Ext JS Cookbook. This book provides clear instructions for getting the most out of Ext JS with and offers many exercises to build impressive rich internet applications. This cookbook shows techniques and "patterns" for building particular interface styles and features in Ext JS. Pick what you want and move ahead. It teaches you how to use all of the Ext JS widgets and components smartly, through practical examples and exercises. Native and custom layouts, forms, grids, listviews, treeviews, charts, tab panels, menus, toolbars, and many more components are covered in a multitude of examples.The book also looks at best practices on data storage, application architecture, code organization, presenting recipes for improving themóour cookbook provides expert information for people working with Ext JS.
Table of Contents (15 chapters)
Ext JS 3.0 Cookbook
Credits
About the Author
About the Reviewer
Preface

Adding features to the Ext JS classes


It is possible to add new functions to the Ext JS classes, as well as modify the behavior of the native functions. To illustrate this point, this recipe explains how you can modify the MixedCollection class so that it features a new function which allows items to be added only when they don't already exist in the collection.

How to do it...

The following example shows how to add a new function to the MixedCollection class:

  1. 1. Define the new addUnique(key, object) function within the MixedCollection class:

    // Add a function to the MixedCollection Class.
    Ext.override(Ext.util.MixedCollection, {
    addUnique: function(key, object) {
    if (this.indexOf(object) > -1) return;
    this.add(key, object);
    }
    });
    
  2. 2. Now, we can use the new feature here:

    Ext.onReady(function() {
    // Create our enhanced collection.
    var col = new Ext.util.MixedCollection();
    // Confirm that the same value cannot be added twice.
    col.add("key 1", "value 1");
    document.write("Original count: " + col.getCount() + "<br/>");
    // Use the added function to make sure duplicates are not
    //added.
    col.addUnique("key 2", "value 1");
    // Number of elements in the collection is still 1.
    document.write("Count after trying to add a duplicate: " + col.getCount() + "<br/>");
    });
    
    

How it works...

The magic in this recipe is achieved through the use of Ext.override(originalClass, overrides). This function adds a list of functions to the prototype of an existing class, replacing any existing methods with the same name:

override: function(origclass, overrides) {
if (overrides) {
var p = origclass.prototype;
Ext.apply(p, overrides);
if (Ext.isIE && overrides.toString != origclass.toString) {
p.toString = overrides.toString;
}
}
}

There's more...

Using Ext.override(originalClass, overrides), it is also possible to modify the behavior of a class's native functions.

Let's modify the add(key, object) function of the MixedCollection class so that only unique values can be added to the collection.

Use Ext.override(originalClass, overrides) to redefine the add function as shown in the following code:

Ext.override(Ext.util.MixedCollection, {
// The new add function, with the unique value check.
add: function(key, o) {
// The unique value check.
if (this.indexOf(o) > -1) return null;
//From this point, the code is the add function's original
//code.
if (arguments.length == 1) {
o = arguments[0];
key = this.getKey(o);
}
if (typeof key == "undefined" || key === null) {
this.length++;
this.items.push(o);
this.keys.push(null);
} else {
var old = this.map[key];
if (old) {
return this.replace(key, o);
}
this.length++;
this.items.push(o);
this.map[key] = o;
this.keys.push(key);
}
this.fireEvent("add", this.length - 1, o, key);
return o;
}
});

Now, we can use the new behavior:

Ext.onReady(function() {
// Create our enhanced collection.
var col = new Ext.util.MixedCollection();
// Confirm that the same value cannot be added twice.
col.add("key 1", "value 1");
document.write("Original count: " + col.getCount() + "<br/>");
// Try to add a duplicate.
col.add("key 2", "value 1");
// Number of elements in the collection is still 1.
document.write("Count after trying to add a duplicate: " + col.getCount() + "<br/>");
});

See also...

  • The next recipe, Building custom JavaScript classes that inherit the functionality of Ext JS, explains how to incorporate Ext JS's features into your custom classes.