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

Determining the object type and converting empty references to a default value


This recipe teaches you how to determine the types of different objects using the facilities of Ext JS, as well as a simple method that can be used to initialize empty references with a default value.

How to do it...

You can determine the types of different objects in the following way:

  1. 1. Create some dummy data structures:

    var colorsArray = new Array();
    colorsArray[0] = 'Blue';
    colorsArray[1] = 'Red';
    colorsArray[2] = 'White';
    var colorsObject = { color1: 'Blue', color2: 'Red', color3: 'White' };
    var aNumber = 1;
    var aString = '1';
    var sample;
    var empty;
    
  2. 2. Check the types of our variables:

    var colorsArrayType = Ext.type(colorsArray);
    // colorsArrayType's value is "array".
    var isArray = Ext.isArray(colorsArray);
    // isArray is true
    var colorsObjectType = Ext.type(colorsObject);
    // colorsObjectType's value is "object".
    var isArray = Ext.isArray(colorsObject);
    // isArray is false
    var number = Ext.num(aNumber, 0);
    // number is 1.
    number = Ext.num(aString, 0);
    // Since aString is not numeric, the supplied
    // default value (0) will be returned.
    var defined = Ext.util.Format.undef(sample);
    // defined is an empty string
    sample = "sample is now defined";
    defined = Ext.util.Format.undef(sample);
    // defined is now "sample is now defined".
    var notEmpty = Ext.value(empty, 'defaultValue', false);
    // notEmpty is 'defaultValue'
    

How it works...

The Ext.type(object) function is capable of detecting elements, objects, text nodes, whitespaces, functions, arrays, regular expressions, numbers, and node lists.

As its name indicates, Ext.isArray(object) simply checks whether the passed object is a JavaScript array. Ext.num(value, defaultValue), in turn, does a numeric type check on the passed value and returns the default value when the argument is not numeric.

Ext.util.Format.undef(value) is a very useful function when you need to test for undefined values. It returns either the supplied argument or an empty string if the argument is undefined.

Ext.value(value, defaultValue, allowBlank) also allows you to specify a default value when the value being tested is undefined.