Ext JS 4 Web Application Development Cookbook


Ext JS 4 Web Application Development Cookbook Over 110 easy-to-follow recipes backed up with real-life examples, walking you through basic Ext JS features to advanced application design using Sencha's Ext JS Stuart Ashworth Andrew Duncan BIRMINGHAM - MUMBAI This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Ext JS 4 Web Application Development Cookbook Copyright © 2012 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: August 2012 Production Reference: 1170812 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-84951-686-0 www.packtpub.com Cover Image by Ed Maclean (edmaclean@gmail.com) This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Credits Authors Stuart Ashworth Andrew Duncan Reviewers Aafrin Fareeth Yiyu Jia Peter Kellner Joel Watson Acquisition Editor Usha Iyer Lead Technical Editor Dayan Hyames Technical Editors Apoorva Bolar Madhuri Das Project Coordinator Michelle Quadros Proofreader Martin Diver Indexer Hemangini Bari Graphics Manu Joseph Production Coordinators Shantanu Zagade Aparna Bhagat Cover Work Shantanu Zagade This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 About the Authors Stuart Ashworth is a professional web developer and an all-round web geek currently living in Glasgow, Scotland with his girlfriend Sophie and wee dog, Meg. After graduating with a first-class honors degree in Design Computing from the University of Strathclyde, he earned his stripes at a small software company in the city. Stuart has worked with Sencha technologies for over three years, creating various large and small-scale web applications, mobile applications, and framework plugins along the way. At the end of 2010, Stuart and Andrew formed SwarmOnline, later becoming an official Sencha partner. Since then they have worked on projects with a number of local, national, and international clients ranging from small businesses to large multinational corporations. Stuart enjoys playing football, snowboarding, and visiting new cities. He blogs about Sencha technologies on the SwarmOnline website as much as possible and can be contacted through Twitter, e-mail, or the Sencha forums. Andrew Duncan’s passion for the Internet and web development began from a young age, where he spent much of his time creating websites and installing/managing a 2 km square wireless mesh network for his local, rural community. After graduating in Business and Management from the University of Glasgow, Andrew was inspired to set up a business offering web development, training, and consultancy as SwarmOnline. During expansion, he partnered with Stuart at the end of 2010. His experience is now expansive, having worked with a large variety of small, medium, and multinational businesses for both the public and private-sector markets. Sencha’s technologies first became of interest to Andrew more than three years ago. His knowledge and enthusiasm was recognized in the Sencha Touch App contest where SwarmOnline secured a top 10 place. This talent did not go unrecognized as Sencha soon signed SwarmOnline as their first official partner outside the US. When not immersed in technology, Andrew lives in Glasgow’s West End with his girlfriend, Charlotte. He enjoys skiing, curling, and DIY projects. Andrew can be found on swarmonline. com/blog, by e-mail, and on Twitter. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 About the Reviewers Aafrin Fareeth is a self-made programmer who fell in love with codes during his high school. Since then he has mastered several languages, such as C++, Java, PHP, ASP, VB, VB.NET, and is on a quest to master more languages. He specializes in web application development, security testing, and forensic analysis. I would like to thank my family and friends who have been very supportive, Nor Hamirah for her continuous encouragement and motivation, Jovita Pinto, and Reshma Sundaresan for this wonderful opportunity. Yiyu Jia has been developing web applications since 1996. He worked as a technical leader and solutions architect on various projects with Java and PHP as the major backend languages. He also has professional experience in interactive TV middleware and home gateway projects. He is especially interested in designing multi-channel web applications. Yiyu Jia is also the main founder of the novel data-mining research topic—Promotional Subspace Mining (PSM), which aims at finding out useful information from subspaces in very large data sets. He can be reached at the given e-mail address—yiyu.jia@gmail.com. His blog and website are http://yiyujia.blogspot.com and http://www.idatamining. org respectively. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Peter Kellner, a Microsoft ASP.NET MVP since 2007, is founder and president of ConnectionRoad, and a seasoned software professional specializing in high quality, scalable, and extensible web applications. His experience includes building and leading engineering teams both on and off shore. Peter is actively engaged in the software community being the primary leader of Silicon Valley Code Camp, which attracted over 2,000 people in 2011 with over 200 sessions. He also organizes the San Francisco Sencha Users Group. In his free time he and his wife Tammy can be found biking the Santa Cruz Mountains. In 2003 they rode across the United States in 27 days. Joel Watson is a web enthusiast, working for the past eight years in website design and development. He loves exploring web technologies of all sorts, and particularly enjoys creating web experiences that leverage the newest features of HTML5 and its related technologies. When he’s not coding, Joel enjoys spending time with his wife and two daughters, playing guitar, and watching cheesy sci-fi and anime. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 www.PacktPub.com Support files, eBooks, discount offers, and more You might want to visit www.PacktPub.com for support files and downloads related to your book. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com, and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt’s online digital book library. Here, you can access, read, and search across Packt’s entire library of books.  Why Subscribe? ff Fully searchable across every book published by Packt ff Copy and paste, print, and bookmark content ff On demand and accessible via web browser Free Access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 For Charlotte, Sophie, and our families. Thank you for the support and encouragement you gave us while writing this book. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Table of Contents Preface 1 Chapter 1: Classes, Object-Oriented Principles and Structuring your Application 7 Introduction 8 Creating custom classes using the new Ext JS class system 8 Using inheritance in your classes 15 Adding mixins to your class 19 Scoping your functions 22 Dynamically loading Ext JS classes 27 Aliasing your components 29 Accessing components with component query 31 Extending Ext JS components 37 Overriding Ext JS' functionality 40 Chapter 2: Manipulating the Dom, Handling Events, and Making AJAX Requests 45 Introduction 46 Selecting DOM elements 46 Traversing the DOM 49 Manipulating DOM elements 51 Creating new DOM elements 55 Handling events on elements and components 58 Delegating event handling of child elements 60 Simple animation of elements 64 Custom animations 67 Parsing, formatting, and manipulating dates 70 Loading data with AJAX 73 Encoding and decoding JSON data 75 This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 ii Table of Contents Chapter 3: Laying Out Your Components 79 Introduction 79 Using a FitLayout to expand components to fill their parent 80 Creating flexible vertical layouts with VBoxes 82 Creating flexible horizontal layouts with HBoxes 85 Displaying content in columns 88 Collapsible layouts with accordions 91 Displaying stacked components with CardLayouts 94 Anchor components to their parent's dimensions 98 Creating fullscreen applications with the BorderLayout 103 Combining multiple layouts 107 Chapter 4: UI Building Blocks—Trees, Panels, and Data Views 113 Introduction 114 Loading a tree's nodes from the server 114 Sorting tree nodes 117 Dragging-and-dropping nodes within a tree 120 Using a tree as a menu to load content into another panel 123 Docking items to panels' edges 126 Displaying a simple form in a window 130 Creating a tabbed layout with tooltips 132 Manipulating a tab panel's TabBar 134 Executing inline JavaScript to in an XTemplate customize appearance 138 Creating Ext.XTemplate member functions 140 Adding logic to Ext.XTemplates 144 Formatting dates within an Ext.XTemplate 146 Creating a DataView bound to a data store 147 Displaying a detailed window after clicking a DataView node 152 Chapter 5: Loading, Submitting, and Validating Forms 157 Introduction 157 Constructing a complex form layout 158 Populating your form with data 163 Submitting your form's data 167 Validating form fields with VTypes 170 Creating custom VTypes 171 Uploading files to the server 175 Handling exception and callbacks 178 Chapter 6: Using and Configuring Form Fields 183 Introduction 183 Displaying radio buttons in columns 184 Populating CheckboxGroups 189 This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 iii Table of Contents Dynamically generate a CheckboxGroup from JSON 193 Setting up available date ranges in Date fields 199 Loading and parsing Dates into a Date field 202 Entering numbers with a Spinner field 204 Sliding values using a Slider field 207 Loading server side data into a combobox 210 Autocompleting a combobox's value 212 Rendering the results in a combobox 216 Rich editing with an HTML field 219 Creating repeatable form fields and fieldsets 221 Combining form fields 224 Chapter 7: Working with the Ext JS Data Package 229 Introduction 229 Modeling a data object 230 Loading and saving a Model using proxies 234 Loading cross-domain data with a Store 238 Associating Models and loading nested data 241 Applying validation rules to Models' fields 248 Grouping a Store's data 253 Handling Store exceptions 259 Saving and loading data with HTML5 Local Storage 262 Chapter 8: Displaying and Editing Tabular Data 265 Introduction 265 Displaying simple tabular data 266 Editing grid data with a RowEditor 269 Adding a paging toolbar for large datasets 276 Dealing with large datasets with an infinite scrolling grid 278 Dragging-and-dropping records between grids 282 Creating a grouped grid 288 Custom rendering of grid cells with TemplateColumns 291 Creating summary rows aggregating the grid's data 295 Displaying full-width row data with the RowBody feature 300 Adding a context menu to grid rows 304 Populating a form from a selected grid row 308 Adding buttons to grid rows with action columns 312 Chapter 9: Constructing Toolbars with Buttons and Menus 319 Introduction 319 Creating a split button 319 Working with context menus 324 This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 iv Table of Contents Adding a combobox to a toolbar to filter a grid 328 Using the color picker in a menu 333 Chapter 10: Drawing and Charting 337 Introduction 337 Drawing basic shapes 338 Applying gradients to shapes 343 Drawing paths 346 Transforming and interacting with shapes 352 Creating a bar chart with external data 356 Creating a pie chart with local data 360 Creating a line chart with updating data 365 Customizing labels, colors, and axes 370 Attaching events to chart components 375 Creating a live updating chart bound to an editable grid 379 Chapter 11: Theming your Application 383 Introduction 383 Compiling SASS with Compass 384 Introduction to SASS 388 Using Ext JS' SASS variables 395 Using the UI config option 398 Creating your own theme mixins 403 Restyling a panel 406 Creating images for legacy browsers 410 Chapter 12: Advanced Ext JS for the Perfect App 413 Introduction 414 Advanced functionality with plugins 414 Architecting your applications with the MVC pattern 420 Attaching user interactions to controller actions 424 Creating a real-life application with the MVC pattern 431 Building your application with Sencha's SDK tools 441 Getting started with Ext Direct 445 Loading and submitting forms with Ext Direct 449 Handling errors throughout your application 455 Index 459 This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Preface 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 customizable themes, and plugin-free charting. This book works through the framework from the fundamentals to advanced features and application design. More than 110 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, you will learn how to develop truly interactive and responsive web applications. 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. 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. The Ext JS 4 Web Application Development Cookbook will provide you with the knowledge to create interactive and responsive web applications, using real-life examples. What this book covers Chapter 1, Classes, Object-Oriented Principles, and Structuring your Application, covers how to harness the power of Ext JS 4's new class system, architect your application using the Model-View-Controller (MVC) pattern, and extend the framework's functionality. Chapter 2, Manipulating the Dom, Handling Events, and Making AJAX Requests, covers topics such as working with the Document Object Model (DOM), selecting, creating, and manipulating elements. We'll look at how to add built-in animations to your elements and how to create custom animations. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Preface 2 We'll talk through creating your first AJAX request and encoding/decoding the data either in JSON or HTML format. Other topics include handling events, working with dates, detecting browser features, and evaluating object types/values. Chapter 3, Laying Out your Components, explores the layout system in Ext JS 4 and demonstrates how to use these layouts to place your user-interface components. The layouts we will work with are FitLayout, BorderLayout, HBoxLayout, VBoxLayout, ColumnLayout, TableLayout, AccoridionLayout, CardLayout, AnchorLayout, and AbsoluteLayout. The final recipe will combine a number of these layouts to create a framework for a rich desktop-style application. Chapter 4, UI Building Blocks – Trees, Panels, and Data Views, looks at how creating and manipulating the basic components that Ext JS provides is fundamental to producing a rich application. In this chapter, we will cover three fundamental Ext JS UI components and explore how to configure and control them within your applications. Chapter 5, Loading, Submitting, and Validating Forms, introduces forms in Ext JS 4. We begin by creating a support-ticket form in the first recipe. Instead of focusing on how to configure specific fields, we demonstrate more generic tasks for working with forms. Specifically, these are populating forms, submitting forms, performing client-side validation, and handling callbacks/exceptions. Chapter 6, Using and Configuring Form Fields, will focus on how we configure and use Ext JS 4's built-in form fields and features to hone our forms for a perfect user experience. We will cover various form fields and move up from configuring the fields using their built-in features to customizing the layout and display of these fields to create a form that creates a smooth and seamless user experience. Chapter 7, Working with the Ext JS Data Package, will cover the core topics of the Data Package. In particular, we will demonstrate Models, Stores, and Proxies, and explain how each is used for working with your applications' structured data. Chapter 8, Displaying and Editing Tabular Data, will cover the basics of simple grids to advanced topics such as infinite scrolling and grouping. We will also demonstrate how to edit data easily, customize how we present data, and link your grids with other Ext JS components. Chapter 9, Constructing Toolbars with Buttons and Menus, looks at toolbars, buttons, and menus as they are the foundation for giving users the means to interact with our applications. They are a navigation and action-launching paradigm that almost all computer users are familiar with, and so making use of them in your applications will give users a head start in finding their way around. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Preface 3 This chapter will explore these crucial components and demonstrate how to add them to your application to provide an interactive and dynamic user experience. Chapter 10, Drawing and Charting, will demonstrate the new charting and drawing features introduced to Ext JS 4. In particular, you will discover how to chart data for presentation in numerous ways. We will take you through the Ext.draw package which, as you will learn, is used as the basis of the charting package that we explore later. The first recipes introduce the tools available for drawing shapes and text before moving onto the fully featured Ext.chart classes that enable you to quickly create and integrate attractive, interactive charts into your apps. Chapter 11, Theming your Application, describes the tasks involved in customizing the look and feel of your Ext JS application. You will learn the basics of SASS and Compass and move on to compiling the framework's SASS. We will then explore how to customize your theme with SASS options and custom mixins. Finally we will demonstrate how to take care of legacy browsers using the Sencha SDK Tools' slicer tool. Chapter 12, Advanced Ext JS for the Perfect App, covers advanced topics in Ext JS that will help make your application stand out from the crowd. We will start by explaining how to extend and customize the framework through plugins where we will write a plugin to toggle textfields between an editable and display state. The next recipes will focus on the MVC pattern that has become the recommended way of structuring your applications. These recipes will start by explaining the file and class structure we need leading into how to connect your application's parts together. Finally we will take one of our earlier examples and demonstrate how to create it while following the MVC pattern. We will also focus on Ext.Direct and how it can be used to handle server communications in conjunction with forms and stores. Other advanced topics such as state, advanced exception handling, history management, and task management will also be described. Appendix, Ext JS 4 Cookbook - Exploring Further, contains an additional 20 recipes with more useful hints and tips to help you to get the most out of Sencha's Ext JS 4 framework. Following the same format as the book, these extra recipes cover a wide variety of topics and we hope they further broaden your knowledge of the framework. This appendix is not present in the book but is available as a free download from http://www.packtpub.com/sites/default/ files/downloads/Appendix_Ext JS 4 Cookbook_Exploring Further.pdf. What you need for this book Before getting started with this book make sure you have your favorite text editor ready and a browser with some developer tools and a JavaScript debugger. We recommend Google Chrome (with Developer Tools) or Firefox (with Firebug). This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Preface 4 All the recipes require the Ext JS 4 SDK. This is available as a free download from Sencha's website http://www.sencha.com/products/extjs/. Additionally, some make use of the Sencha SDK Tools, which can be downloaded from http://www.sencha.com/products/sdk-tools/. Although each recipe is a standalone example, we need to include the SDK and add the Ext. onReady method to our HTML file, which will execute the passed function when everything is fully loaded. Prepare an HTML file with the following, which can be used as the starting point for most of the recipes: The example source code supplied with this book can be executed as a standalone project or by importing each chapter's folder into the Ext JS SDK package's examples folder. Who this book is for The Ext JS 4 Web Application Development Cookbook is aimed at both newcomers and those experienced with Ext JS who want to expand their knowledge and learn how to create interactive web applications with Ext JS 4. Conventions In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning. Code words in text are shown as follows: "We can include other contexts through the use of the include directive." This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Preface 5 A block of code is set as follows: Ext.define('Cookbook.Smartphone', { mixins: { camera: 'HasCamera' } }); When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold: ... fields: [... { name: 'PublishDate', type: 'date', dateFormat: 'd-m-Y' } ...] ... Any command-line input or output is written as follows: Windows: gem install compass Mac OS X: sudo gem install compass New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "The repeated questions will be dynamically added to the form by pressing an Add Another Guest button." Warnings or important notes appear in a box like this. Tips and tricks appear like this. Reader feedback Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Preface 6 To send us general feedback, simply send an e-mail to feedback@packtpub.com, and mention the book title through the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors. Customer support Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase. Downloading the example code You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you. Errata Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website, or added to any list of existing errata, under the Errata section of that title. Piracy Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy. Please contact us at copyright@packtpub.com with a link to the suspected pirated material. We appreciate your help in protecting our authors, and our ability to bring you valuable content. Questions You can contact us at questions@packtpub.com if you are having a problem with any aspect of the book, and we will do our best to address it. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 1 Classes, Object-Oriented Principles and Structuring your Application In this chapter, we will cover: ff Creating custom classes using the new Ext JS class system ff Using inheritance in your classes ff Adding mixins to your classes ff Scoping your functions ff Dynamically loading Ext JS classes ff Aliasing your components ff Accessing components with component query ff Extending Ext JS components ff Overriding Ext JS functionality This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Classes, Object-Oriented Principles and Structuring your Application 8 Introduction In this chapter, you will learn how to harness the power of Ext JS 4's new class system, and extend the framework's functionality. Creating custom classes using the new Ext JS class system Although JavaScript is not a class-based language, it is possible to simulate classes using its prototypal structure. Ext JS 4 introduces an entirely new way of defining classes, compared with Ext JS 3. Consequently, when developing with Ext JS 4 your JavaScript's structure will be more closely in line with that of other object oriented languages. This recipe will explain how to define classes using the new system, and give some detail about the features it has to offer. We will do this by creating a custom class to model a vehicle, with a method that will alert some details about it. How to do it... The Ext.define method is used to define new classes. It uses a string-based definition, leaving the framework to take care of the namespacing and concrete defining of the class: 1. Call the Ext.define method with our class name and configuration object. // Define new class 'Vehicle' under the 'Cookbook' namespace Ext.define('Cookbook.Vehicle', { // class configuration goes here }); 2. Add properties and methods to the configuration object: Ext.define('Cookbook.Vehicle', { Manufacturer: 'Aston Martin', Model: 'Vanquish', getDetails: function(){ alert('I am an ' + this.Manufacturer + ' ' + this.Model); } }); Downloading the example code You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/ support and register to have the files e-mailed directly to you. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 1 9 3. We now add the Ext.define method's optional third parameter, which is a function that is executed after the class has been defined, within the scope of the newly created class: Ext.define('Cookbook.Vehicle', { Manufacturer: 'Aston Martin', Model: 'Vanquish', getDetails: function(){ alert('I am an ' + this.Manufacturer + ' ' + this.Model); } }, function(){ Console.log('Cookbook.Vehicle class defined!'); }); 4. Finally, we create an instance of the new class and call its getDetails method: var myVehicle = Ext.create('Cookbook.Vehicle'); alert(myVehicle.Manufacturer); // alerts 'Aston Martin' myVehicle.getDetails(); // alerts 'I am an Aston Martin Vanquish' How it works... 1. The Ext.define method handles the creation and construction of your class, including resolving the namespaces within your class name. Namespaces allow us to organize classes into logical packages to keep code organized and prevents the global scope from becoming polluted. In our example, Ext JS will create a package (essentially just an object) called Cookbook, which contains our Vehicle class as a property. Your namespaces can be infinitely deep (that is, as many dots as you wish) and are automatically created by the framework. 2. The first parameter of this method identifies the class name as a string. Class names are always given as strings (when defined and when instantiated) so they can be dynamically loaded when needed, meaning you can start to instantiate a class before it has been loaded. 3. The second parameter of this method accepts a standard JavaScript object that defines all of the properties and methods of your class. These can be accessed, as you would expect, from an instance of the class. 4. The third parameter of Ext.define's is an optional callback function that gets executed once the class has been fully defined and is ready to be instantiated. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Classes, Object-Oriented Principles and Structuring your Application 10 5. Internally every class that is defined is turned into an instance of the Ext.Class class by the Ext.ClassManager. During this process, the manager runs through a series of pre and post processing steps. These processors each take care of initializing one part of the class and are called in the following order: ‰‰ Loader: Loads any other required classes if they don't already exist, recursing through this process for each class loaded ‰‰ Extend: Now that all the required classes have been loaded, we can extend from them as required by our extend config option ‰‰ Mixins: Any Mixins that have been defined are now handled and merged into our class ‰‰ Config: Any properties in the config configuration option are processed and their get/set/apply/reset methods are created ‰‰ Statics: If the class has any static properties or methods these are handled at this stage 6. Once all of these pre-processors have completed their work our new class is ready to be instantiated. However, it will continue to work through its post-processors that perform the following actions: ‰‰ Aliases: It creates the necessary structure to allow the class to be created through an xtype ‰‰ Singleton: If the class has been defined as a singleton its single instance is created here ‰‰ Legacy: To help with backward compatibility a class can have alternate names that are mapped to the class At this point our class is fully created, and all that is left to do is to execute the callback function (defined as the third parameter to Ext.define) to signal the class definition being complete. The full process can be seen in the following diagram: Ext.Class Ext.define Callback Pre-Processors Post-Processors Loader Aliases Singleton Legacy Extend Mixins Config Statics This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 1 11 This model is extremely flexible and allows you to include your own pre or post processor at any stage in the sequence by using the registerPreProcessor and registerPostProcessor methods. All Ext JS 4 classes inherit from a common base class, named Ext.Base. This class contains several methods that provide basic functionality to all created subclasses, for example override and callParent. When we define a new class using the Ext.define method, and don't specify an explicit base class, then the framework will automatically use Ext.Base as its base inside the Extend preprocessor. If we do specify a base class then that class will, at the root of its inheritance tree, extend Ext.Base. The following diagram shows how our custom class fits into this structure: Ext.Base Cookbook.Vehhicle extends There's more... The new Ext JS class system also takes care of a lot of the heavy lifting for you when it comes to defining your properties, configuration options, and their associated getter and setter methods. If you define these configuration options within a config object, the class system (inside its Config pre-processor) will automatically generate get, set, reset, and apply methods. This reduces the amount of code that needs to be maintained and downloaded. The following code sample utilizes this config option and takes advantage of the free code that the framework will create. This code is initialized by calling the initConfig method within the constructor, which is executed when your class is instantiated. Constructors are special methods that are executed when a class is instantiated (either using the Ext.create(..) or new syntax) and are used to prepare the object in any way needed. For example, it could be used to set up default property values. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Classes, Object-Oriented Principles and Structuring your Application 12 Ext.define('Cookbook.Vehicle', { config: { Manufacturer: 'Aston Martin', Model: 'Vanquish' }, constructor: function(config){ // initialise our config object this.initConfig(config); }, getDetails: function(){ alert('I am an ' + this.Manufacturer + ' ' + this.Model); } }); // create a new instance of Vehicle class var vehicle = Ext.create('Cookbook.Vehicle'); // display its details vehicle.getDetails(); // update Vehicle details vehicle.setManufacturer('Volkswagen'); vehicle.setModel('Golf'); // display its new details vehicle.getDetails(); By using this approach it is the equivalent of defining your class with the explicit methods shown as follows: Ext.define('Cookbook.Vehicle', { Manufacturer: 'Aston Martin', Model: 'Vanquish', getManufacturer: function(){ return this.Manufacturer; }, setManufacturer: function(value){ this.Manufacturer = value; }, This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 1 13 resetManufacturer: function(){ this.setManufacturer('Aston Martin'); }, applyManufacturer: function(manufacturer){ // perform some action to apply the value (e.g. update a DOM element) return manufacturer; }, getModel: function(){ return this.Model; }, setModel: function(value){ this.Model = value; }, resetModel: function(){ this.setModel('Vanquish'); }, applyModel: function(model){ // perform some action to apply the value (e.g. update a DOM element) return model; }, getDetails: function(){ alert('I am an ' + this.Manufacturer + ' ' + this.Model); } }); Notice that we return the property's value within our apply methods. This is important as this method is called by the property's set method, so the new value is applied appropriately, and its return value is stored as the property's value. Sometimes we will want to perform some extra actions when calling these generated methods. We can do this by explicitly defining our own version of the method that will override the generated one. In our example, when calling the apply method, we want to update a DOM element that contains the Vehicle's name, so the change is reflected on the screen. First we add some markup to hold our Vehicle's data: This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Classes, Object-Oriented Principles and Structuring your Application 14 Now we override the applyManufacturer and applyModel methods to perform an update of each DOM element when the properties are changed: Ext.define('Cookbook.Vehicle', { config: { Manufacturer: 'Aston Martin', Model: 'Vanquish' }, constructor: function(config){ // initialise our config object this.initConfig(config); }, getDetails: function(){ alert('I am an ' + this.getManufacturer() + ' ' + this. getModel()); }, applyManufacturer: function(manufacturer){ Ext.get('manufacturer').update(manufacturer); return manufacturer; }, applyModel: function(model){ Ext.get('model').update(model); return model; } }); // create a Vehicle and set its Manufacturer and Model var vehicle = Ext.create('Cookbook.Vehicle'); vehicle.setManufacturer('Volkswagen'); vehicle.setModel('Golf'); See also ff The next recipe explaining how to include inheritance in your classes. ff The Adding mixins to your class recipe, which describes what Mixins are and how they can be added to your classes. ff Dynamically Loading ExtJS Classes which explains how to use the dynamic dependency loading system that the framework provides. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 1 15 Using inheritance in your classes More often than not when defining a new class, we want to extend an existing Ext JS class or component so that we inherit its current behavior and add our own new functionality. This recipe explains, how to extend an existing class and add new functionality through new methods and by overriding existing ones. We will define a very simple class that models a Vehicle, capturing its Manufacturer, Model, and Top Speed. It has one method called travel, which accepts a single parameter that represents the distance to be travelled. When called, it will show an alert with details of the vehicle, how far it travelled, and at what speed. How to do it... 1. Define our base Vehicle class, which provides us with our basic functionality and from which we will extend our second class: Ext.define('Cookbook.Vehicle', { config: { manufacturer: 'Unknown Manufacturer', model: 'Unknown Model', topSpeed: 0 }, constructor: function(manufacturer, model, topSpeed){ // initialise our config object this.initConfig(); if(manufacturer){ this.setManufacturer(manufacturer); } if(model){ this.setModel(model); } if(topSpeed){ this.setTopSpeed(topSpeed); } }, travel: function(distance){ alert('The ' + this.getManufacturer() + ' ' + this. getModel() + ' travelled ' + distance + ' miles at ' + this. getTopSpeed() + 'mph'); } This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Classes, Object-Oriented Principles and Structuring your Application 16 }, function(){ console.log('Vehicle Class defined!'); }); var vehicle = Ext.create('Cookbook.Vehicle', 'Aston Martin', 'Vanquish', 60); vehicle.travel(100); // alerts 'The Aston Martin Vanquish travelled 100 miles at 60mph 2. Define a sub-class Cookbook.Plane that extends our base Vehicle class and accepts a fourth parameter of maxAltitude: Ext.define('Cookbook.Plane', { extend: 'Cookbook.Vehicle', config: { maxAltitude: 0 }, constructor: function(manufacturer, model, topSpeed, maxAltitude){ // initialise our config object this.initConfig(); if(maxAltitude){ this.setMaxAltitude(maxAltitude); } // call the parent class' constructor this.callParent([manufacturer, model, topSpeed]); } }, function(){ console.log('Plane Class Defined!'); }); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 1 17 3. Create an instance of our Cookbook.Plane sub-class and demonstrate that it has the properties and methods defined in both the Vehicle and Plane classes: var plane = Ext.create('Cookbook.Plane', 'Boeing', '747', 500, 30000); plane.travel(800); Alerts The Boeing 747 travelled 800 miles at 500mph (inherited from the Vehicle class) alert('Max Altitude: ' + plane.getMaxAltitude() + ' feet'); Alerts 'MaxAltitude: 30000 feet' (defined in the Plane class) How it works... The extend configuration option, used when defining your new subclass, tells the Ext.Class' Extend preprocessor (which we talked about in the previous recipe) what class your new one should be inherited from. The preprocessor then merges all of the parent class' members into the new class' definition, giving us our extended class. By extending the Vehicle class in this way our class diagram will look like the one shown as follows. Notice that the Plane class still inherits from the Ext.Base class through the Vehicle class' extension of it: Ext.Base Cookbook.Vehhicle extends Cookbook.Plane extends This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Classes, Object-Oriented Principles and Structuring your Application 18 The callParent method is a very quick way of executing the parent class' version of the method. This is important to ensure that the parent class is constructed correctly and will still function as we expect. In previous versions of Ext JS, this was achieved by using the following syntax: Plane.superclass.constructor.apply(this, arguments); The new callParent method effectively still does this but it is hidden from the developer, making it much easier and quicker to call. There's more... We can expand on this idea by adding new functionality to the Plane class and override the base class' travel method to incorporate this new functionality. A plane's travel method is a little more complicated than a generic vehicle's so we're going to add takeOff and land methods to the class: Ext.define('Cookbook.Plane', { ... takeOff: function(){ alert('The ' + this.getManufacturer() + ' ' + this.getModel() + ' is taking off.'); }, land: function(){ alert('The ' + this.getManufacturer() + ' ' + this.getModel() + ' is landing.'); } ... }); We can then override the travel method of the Vehicle class to add in the takeOff and land methods into the Plane's travel procedure: Ext.define('Cookbook.Plane', { ... travel: function(distance){ this.takeOff(); // execute the base class’ generic travel method this.callParent(arguments); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 1 19 alert('The ' + this.getManufacturer() + ' ' + this.getModel() + ' flew at an altitude of ' + this.getMaxAltitude() + 'feet'); this.land(); } ... }); This method extends the travel functionality given to us by the Vehicle class by alerting us to the fact that the plane is taking off, flying at a specific altitude, and then landing again. The important part of this method is the call to the callParent method. This executes the base class' travel method, which runs the Vehicle's implementation of the travel method. Notice that it passes in the arguments variable as a parameter. This variable is available in all JavaScript functions and contains an array of all the parameters that were passed into it. We can see this in action by creating a new Plane object and calling the travel method: var plane = Ext.create('Cookbook.Plane', 'Boeing', '747', 500, 30000); plane.travel(800); // alerts 'The Boeing 747 is taking off' // 'The Boeing 747 travelled 800 miles at 500mph' // 'The Boeing 747 flew at an altitude of 30000 feet' // 'The Boeing 747 is landing.' See also ff The very first recipe in this chapter that covers how classes work. ff The recipe describing Dynamically loading Ext JS classes, which teaches you about how these classes can be loaded on the fly. ff The Extending Ext JS components recipe, which explains how to use inheritance to extend the functionality of the framework. Adding mixins to your class Mixins are classes that can be included in another class, merging its members (methods and properties) into it. This technique provides us with a form of multiple inheritance where the mixin class' methods and properties can be accessed as if they were part of the parent class. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Classes, Object-Oriented Principles and Structuring your Application 20 By making use of mixins we can package small and reusable bits of functionality into an encapsulated class, and merge it into classes which require that functionality. This reduces repetition and removes the need for the class to be extended directly. One example of a Mixin used within the framework is the Ext.form.Labelable class, which gives the component it is mixed into the ability to have a label attached to it. How to do it... 1. Define our simple mixin class called HasCamera with a single method called takePhoto: Ext.define('HasCamera', { takePhoto: function(){ alert('Say Cheese! .... Click!'); } }); 2. Define a skeleton class and use the mixins configuration option to apply our HasCamera mixin to our Cookbook.Smartphone class. Ext.define('Cookbook.Smartphone', { mixins: { camera: 'HasCamera' } }); 3. We can now call our mixin's takePhoto method as part of the Smartphone's class within a useCamera method: Ext.define('Cookbook.Smartphone', { mixins: { camera: 'HasCamera' }, useCamera: function(){ this.takePhoto(); } }); 4. Instantiate the Smartphone class and call the useCamera method: var smartphone = Ext.create('Cookbook.Smartphone'); smartphone.useCamera(); // alerts 'Say Cheese! .... Click!' This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 1 21 How it works... By using the mixins configuration option we tell the class defining process to use the mixins preprocessor to merge all of the mixin class' members into the main class. This now means that all of the methods and properties defined as part of the HasCamera class can be accessed directly from the parent class' instance. The name we give to our mixin in this configuration object allows us to reference it within our class' code. We will explore this later in the recipe. Step 4, shows how we can access the HasCamera class' methods from the parent class by simply calling them as if they are part of the Smartphone class itself. There's more... We might be required to override the functionality provided by our mixins class as we often would when using traditional inheritance. In our example, we might want to introduce a focus routine into the takePhoto process to ensure that our subject is in focus before taking a photo. As we have done in previous recipes, we declare a method called takePhoto that will override the one added by the HasCamera Mixin, and another method to perform our focus operation: Ext.define('Cookbook.Smartphone', { mixins: { camera: 'HasCamera' }, useCamera: function(){ this.takePhoto(); }, takePhoto: function(){ this.focus(); this.takePhoto(); }, focus: function(){ alert('Focusing Subject...'); } }); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Classes, Object-Oriented Principles and Structuring your Application 22 At this point we are in trouble because our new takePhoto method needs to reference the original takePhoto method defined in the HasCamera class. However, at the moment it is pointing back to itself and will cause an infinite loop. We get around this by calling the mixins method directly from its prototype, which can be accessed using the name we assigned it in Step 3. Our takePhoto method now becomes: takePhoto: function(){ this.focus(); this.mixins.camera.takePhoto.call(this); } See also ff The first recipe, Creating custom classes using the new Ext JS class system, for a recap about defining classes. ff Overriding Ext JS' functionality describes how to customize the framework's default behaviour by defining new versions of key methods. ff See the Adding functionality with plugins recipe, in Chapter 12, Advanced Ext JS for the Perfect App to help understand how plugins can be used and how they differ from mixins. Scoping your functions Making sure that you execute your functions in the correct scope is one of the harder tasks faced by new (and experienced!) JavaScript developers. We would recommend studying the scoping rules of JavaScript to get a full understanding of how it works, but we will start this recipe with an explanation of exactly what scope is, how it changes, and how it affects our code. What is Scope? Scope refers to the context that a piece of code is executing in and decides what variables are available to it. JavaScript has two types of scope: global scope and local scope. Variables and functions declared in the global scope are available to code everywhere. Common examples are the document and window variables. Local Scope refers to variables and functions that have been declared within a function, and so are contained by that function. Therefore, they can't be accessed from code above it in the scope chain. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 1 23 The scope chain is the way that JavaScript resolves variables. If you are trying to access a variable within a function, which has not been declared as a local variable within it, the JavaScript engine will traverse back up the chain of functions, (that is, scopes) looking for a variable matching its name. If it finds one then it will be used, otherwise an error will be thrown. This also means that local variables will take precedence over global variables with the same name. We will explore a couple of examples to demonstrate how this works. 1. The first example shows a simple variable being declared in the global scope and it being alerted—no surprises there! var myVar = 'Hello from Global Scope!'; alert(myVar); //alerts 'Hello from Global Scope!' 2. If you run the next example, you will now see two alerts; the first will say Hello from Global Scope! and the second Hello from MyFunction!. Our myFunction function is able to access the myVar variable because it was declared in the global scope and so can be found on the function's scope chain: var myVar = 'Hello from Global Scope!'; function myFunction(){ myVar = 'Hello from MyFunction!'; } alert(myVar); //alerts 'Hello from Global Scope!' myFunction(); alert(myVar); //alerts 'Hello from MyFunction!' 3. We now add an alert to the myFunction function and add the var keyword in front of the myVar assignment within it. This keyword creates a local variable as part of the myFunction's scope with the same name as the one created in the global scope. The alert inside the myFunction function will now alert Hello from MyFunction! But the two alerts outside the function will alert the original global myVar's value. This is because the myVar variable that was modified in the myFunction function is a new local variable, and so doesn't affect the global version: var myVar = 'Hello Global Scope!'; function myFunction(){ var myVar = 'Hello from MyFunction!'; alert(myVar); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Classes, Object-Oriented Principles and Structuring your Application 24 } alert(myVar); //alerts 'Hello from Global Scope!' myFunction(); //alerts 'Hello from MyFunction!' alert(myVar); //alerts 'Hello from Global Scope!' 4. Finally, we will demonstrate the use of the this keyword. This keyword exists everywhere and provides us with a reference to the context (or scope) that the current piece of code is executing in. Consider the following example where a new object is created using the MyObject's constructor function. If we then console.log the contents of the this keyword, we see that it refers to the new object that we have created. This means that we can define properties on this object and have them contained within this object, and so, inaccessible from any other scope: function MyClass(){ console.log(this); } var myClass = new MyClass(); 5. If we add a property to the this object in our constructor, we can alert it once a new instance has been created. Notice that if we try to alert this.myProperty outside the scope of the MyClass object, it doesn't exist because this now refers to the browser window: function MyClass(){ console.log(this); this.myProperty = 'Hello'; } var myClass = new MyClass(); alert(myClass.myProperty); // alerts 'Hello' alert(this.myProperty); // alerts 'undefined' Scope and Ext JS When dealing with scope in Ext JS we are generally concerned with making sure our functions are executing in the scope of the correct class (whether it is a component, store, or controller). For example, by default an Ext.button.Button's click event will execute its handler function in the scope of itself (that is, this refers to the Ext.button.Button instance). It's likely that we want the button's handler to execute in the scope of the parent class (for example, a grid panel) and so we must force a different scope upon it. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 1 25 We will now explore ways in which we can change the scope a function executes in using Ext JS' in-built functionality. By following these steps we will see how Ext JS makes it easy to ensure this refers to what you want it to! How to do it... Ext JS provides us with a method that allows us to force a function to execute in the scope we specify, meaning we can specify what the this keyword refers to within the function. 1. Define two objects, each with a property and a function: var cat = { sound: 'miaow', speak: function(){ alert(this.sound); } }; var dog = { sound: 'woof', speak: function(){ alert(this.sound); } }; cat.speak(); // alerts 'miaow' dog.speak(); // alerts 'woof' 2. Use the Ext.bind method to force the dog object's speak method to execute in the scope of the cat object by passing it as its second parameter: Ext.bind(dog.speak, cat)(); // alerts 'miaow' How it works... The Ext.bind method creates a wrapper function for the speak method that will force it to have its scope set to the object that is passed in, overriding the default scope value. This new function can be executed immediately (as our example did) or stored in a variable to be executed at a later point. By using it we redefine the this keyword used within the function to refer to what was passed in as the second parameter. This is the reason that in Step 2 the alert displayed the value stored in the cat's sound property rather than the dog's. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Classes, Object-Oriented Principles and Structuring your Application 26 There's more... Getting the scope of a function correct is especially important within event handlers. Ext JS provides a scope config option that can be used to explicitly set the scope an event handler is executed in. Consider the following example where we define a button and attach a handler to its click event, which will show an alert of the current scope's text property: var button = Ext.create('Ext.button.Button', { text: 'My Test Button', listeners: { click: function(button, e, options){ alert(this.text); } }, renderTo: Ext.getBody() }); button.show(); By default, this refers to the button itself and displays My Test Button. But what if we want to execute the function in the scope of another object, like this one? var exampleObject = { text: 'My Test Object' }; Our initial reaction would be to use the Ext.bind method, which we looked at earlier in the recipe, and would look something like this: listeners: { click: Ext.bind(function(button, e, options){ alert(this.text); }, exampleObject) } This technique works well and functions correctly. However, there is a more succinct method in the form of the scope config option, which can be added as shown in the following code: listeners: { click: function(button, e, options){ alert(this.text); }, scope: exampleObject } This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 1 27 The scope object is effectively a short hand way of using Ext.bind and gives us the same outcome with less code. If you were to include multiple event handlers within the listeners property the scope value would be applied to them all. If you want to specify a different scope value for each event, you can use the following syntax: listeners: { click: { fn: function(button, e, options){ alert(this.text); }, scope: this }, afterrender: { fn: function(button, options){ // do something... }, scope: otherObject } } See also ff The recipe Handling event on elements and components in Chapter 2, Manipulating the Dom, Handling Events, and Making AJAX Requests for further examples. 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' } }); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Classes, Object-Oriented Principles and Structuring your Application 28 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. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 1 29 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 ff See the very first recipe covering the details of how to define and work with classes. ff 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. Aliasing your components Aliasing allows you to define a shorthand name for a component class. This is particularly useful as it means you don't always have to reference the full class name, which, if you are following Sencha's naming convention, can become fairly long. Additionally, aliasing allows you to define an xtype for your component. This xtype is not only a shortcut to the full component name but brings advantages such as improved performance. Instead of explicitly creating components during initialization a component with an xtype can be created implicitly as an object config. If you don't instantiate everything as an object, you can defer creation and rendering of the component to save resources until they are actually required. We will demonstrate aliasing by creating a panel inside a viewport. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Classes, Object-Oriented Principles and Structuring your Application 30 How to do it... 1. We start by defining our class and specifying an alias config option with a "widget" prefix: Ext.define('Customer.support.SupportMessage', { extend: 'Ext.panel.Panel', alias: 'widget.supportMessage', title: 'Customer Support', html: 'Customer support is online' }); 2. The panel Customer.support.SupportMessage can be instantiated lazily by using its xtype: Ext.application({ name: 'Customer', launch: function(){ Ext.create('Ext.container.Viewport', { layout: 'fit', items: [{ xtype: 'supportMessage' }] }); } }); How it works... Ext.reg() doesn't exist in Ext JS 4, instead we register aliases with the alias config option. When the application is first loaded, the class definitions are parsed and a dictionary of class aliases is created. This is contained within the framework's component manager (Ext.ComponentManager). We're required to name our aliases with a prefix, "widget." However, when using the alias the prefix is not required. The aliases are, as you would expect, simply a reference to the class. As we haven't instantiated the class (yet) we're able to save memory and resources. This is particularly helpful when our widgets are nested deeply as resources are not wasted on components that are not required or even rendered. As we destroy our components, the framework releases the resources, but keeps the alias reference in the component manager, so that we can re-create the same component time and time again. Calling the alias is done using the xtype config option, which is where you provide the alias name. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 1 31 There's more... There are other ways to define aliases for your components. ff Ext.ClassManager.setAlias(string class, string alias): This registers the alias for a class ff Ext.Base.createAlias(string/object alias, string/object origin): This will create an alias for existing prototype methods. See also ff Aliasing is used throughout this book in a variety of topics and recipes, however, the recipe Constructing a complex form layout, in Chapter 5, Loading, Submitting, and Validating Forms is a good example of using xtypes. Accessing components with component query Ext JS 4 introduces a new helper class called Ext.ComponentQuery, which allows us to get references to Ext JS Components using CSS/XPath style selector syntax. This new class is very powerful and, as you will find out, is leveraged as an integral part of the MVC architecture system. In this recipe we will demonstrate how to use the Ext.ComponentQuery class to get references to specific components within a simple application. We will also move onto exploring how this query engine is integrated into the Ext.Container class to make finding relative references very easy. Finally we will look at adding our own custom selector logic to give us fine-grain control over the components that are retrieved. Getting ready We will start by creating a simple application, which consists of a simple Ext.panel.Panel with a toolbar, buttons, a form, and a grid. This will form the basis of our examples as it has a number of components that we can query for. var panel = Ext.create('Ext.panel.Panel', { height: 500, width: 500, renderTo: Ext.getBody(), This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Classes, Object-Oriented Principles and Structuring your Application 32 layout: { type: 'vbox', align: 'stretch' }, items: [{ xtype: 'tabpanel', itemId: 'mainTabPanel', flex: 1, items: [{ xtype: 'panel', title: 'Users', id: 'usersPanel', layout: { type: 'vbox', align: 'stretch' }, tbar: [{ xtype: 'button', text: 'Edit', itemId: 'editButton' }], items: [{ xtype: 'form', border: 0, items: [{ xtype: 'textfield', fieldLabel: 'Name', allowBlank: false }, { xtype: 'textfield', fieldLabel: 'Email', allowBlank: false }], buttons: [{ xtype: 'button', text: 'Save', action: 'saveUser' }] }, { xtype: 'grid', flex: 1, border: 0, columns: [{ header: 'Name', dataIndex: 'Name', flex: 1 }, { header: 'Email', dataIndex: 'Email' }], This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 1 33 store: Ext.create('Ext.data.Store', { fields: ['Name', 'Email'], data: [{ Name: 'Joe Bloggs', Email: 'joe@example.com' }, { Name: 'Jane Doe', Email: 'jane@example.com' }] }) }] }] }, { xtype: 'component', itemId: 'footerComponent', html: 'Footer Information', extraOptions: { option1: 'test', option2: 'test' }, height: 40 }] }); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Classes, Object-Oriented Principles and Structuring your Application 34 How to do it... The main method of the Ext.ComponentQuery class is the query method. As we have mentioned, it accepts a CSS/XPath style selector string and returns an array of Ext.Component (or subclasses of the Ext.Component class) instances that match the specified selector. 1. Finding components based on xtype: We generally use a component's xtype as the basis for a selector and can retrieve references to every existing component of a xtype by passing it in to the query method. The following snippet will retrieve all Ext.Panel instances: var panels = Ext.ComponentQuery.query('panel'); 2. Just like in CSS we can include the concept of nesting by adding a second xtype separated by a space. In the following example, we retrieve all the Ext.Button instances that are descendants of an Ext.Panel instance: var buttons = Ext.ComponentQuery.query('panel button'); If you have custom classes whose xtypes include characters other than alphanumeric (for example, a dot or hypen) you cannot retrieve them in this way. You must instead query the xtype property of the components using the following syntax: var customXtypeComponents = Ext. ComponentQuery.query('[xtype="My.Custom. Xtype"']; 3. Retrieving components based on attribute values: Along with retrieving references based on xtype, we can query the properties a component possesses to be more explicit about which components we want. In our sample application we have given the Save button an action property to distinguish it from other buttons. We can select this button by using the following syntax: var saveButton = Ext.ComponentQuery.query('button[action="saveUs er"]'); This will return all Ext.Button instances that have an action property with a value of saveUser. 4. Combining selectors: It is possible to combine multiple selectors into one query in order to collect references to components that satisfy two different conditions. We do this by simply comma separating the selectors. The following code will select all Ext.Button and Ext.form.field.Text component instances: var buttonsAndTextfields = Ext.ComponentQuery.query('button, textfield'); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 1 35 5. Finding components based on ID: A component's id and itemId can be included in a selector by prefixing it with the # symbol. This syntax can be combined with all the others we have seen so far but IDs should be unique and so should not be necessary. The following code snippet will select a component with an ID of usersPanel: var usersPanel = Ext.ComponentQuery.query('#usersPanel'); 6. Retrieving components based on attribute presence: One useful feature of the component query engine is that we can select components based on an attribute simply being present, regardless of its value. This can be used when we want to find components that have been configured with specific properties but don't know the values they might have. We can demonstrate this with the following code that will select all Ext.Component that have the property extraOptions. var extraOptionsComponents = Ext.ComponentQuery.query('component[e xtraOptions]'); 7. Using Components' Member Functions: It's also possible to execute a component's member function as a part of the selection criteria. If the function returns a truthy result then that component will be included (assuming the other criteria is met) in the result set. The following code shows this in action and will select all text fields who are direct children of a form and whose isValid method evaluates to true: var validField = Ext.ComponentQuery.query('form > textfield{isValid()}'); How it works... The Ext.ComponentQuery is a singleton class that encapsulates the query logic used in our examples. We have used the query method, which works by parsing each part of the selector and using it in conjunction with the Ext.ComponentManager class. This class is responsible for keeping track of all the existing Ext.Component instances, and is used to find any matching components. There's more... There is one other method of the Ext.ComponentQuery class to introduce and a further four methods that are part of the Ext.container.AbstractContainer class. Evaluating a component instance's type The component query class allows us to evaluate a component reference we already have to find out if it matches a certain criteria. To do this we use the is method, which accepts a selector identical to the ones that the query method accepts and will return true if it does match. The following code determines if our main Ext.Panel (referenced in the panel variable) has an xtype of panel. var isPanel = Ext.ComponentQuery.is(panel, 'panel'); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Classes, Object-Oriented Principles and Structuring your Application 36 Ext.container.AbstractContainer ComponentQuery methods There are four methods available in the Ext.container.AbstractContainer class (which all container classes extend from; for example panels), which utilizes the component query engine and allow us to query using that component as the root. These methods are query, child, up and down. The query method is identical to the query method available in the Ext.ComponentQuery class but uses the container instance as the root of the query and so will only look for components under it in the hierarchy. The up and down methods retrieve the first component, at any level, either above or below the current component in the component hierarchy that matches the selector passed in. Finally, the child method retrieves the first direct child of the current instance that matches the selector. Using and creating the pseudo-selectors Pseudo-selectors allow us to filter the retrieved result array based on some criteria that may be too complex to represent in a plain selector. There are two built-in pseudo-selectors: not and last. These can be added to a selector using a colon. The following example shows a selector that will retrieve the last text field. var lastTextfield = Ext.ComponentQuery.query('textfield:last'); It is very simple for us to create our own custom pseudo-selectors; we will demonstrate how to add a pseudo-selector to retrieve components that are visible. We start by creating a new function on the Ext.ComponentQuery.pseudos object called visible, which accepts one parameter that will contain the array of matches found so far. We will then add code to loop through each item, checking if it's visible and, if it is, adding it to a new filtered array. We then return this new filtered array. Ext.ComponentQuery.pseudos.visible = function(items) { var result = []; for (var i = 0; i < items.length; i++) { if (items[i].isVisible()) { result.push(items[i]); } } return result; }; We can now use this in a selector in the same way as we did before. The following query will retrieve all visible components: var visibleComponents = Ext.ComponentQuery.query('component:visible'); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 1 37 See also ff The recipes about MVC in Chapter 12, Advanced Ext JS for the Perfect App make use of component queries extensively. 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, This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Classes, Object-Oriented Principles and Structuring your Application 38 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' }); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 1 39 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(), '
' + this. infoText + '
'); } }, 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 ff Creating custom classes with the new Ext JS class system for an explanation on creating classes and their structure. ff 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. ff The next recipe covers overriding in more detail. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Classes, Object-Oriented Principles and Structuring your Application 40 Overriding Ext JS' functionality To save the hassle of editing the framework directly (not recommended) when you are looking to alter its behaviour Ext JS provides a very useful override feature. By keeping framework behaviour changes separate you can remove them easily if necessary and keep track of your updates when upgrading to a newer version of the framework. Altering framework code is strongly discouraged as other developers may not realize your changes and be unpleasantly surprised by the non-standard behavior! Overriding allows you to take an existing class and either modify the behavior of existing functions or add completely new ones. This greatly increases the flexibility of the framework as it provides a very straightforward way to completely alter the out-the-box behaviour. Achieving this in Ext JS 4 is done with the Ext.override method, which is an alias of Ext.Base.override. Ext.override(Object originalCls, Object overrides) takes the original class and merges the new (or updated) functions you wish to create for the class. It's perhaps worth pointing out that Ext.override will overwrite any members with the same name, so if you wish to simply extend their functionality you may be required to include the code from the original function. To demonstrate overriding we will add new functions to an existing class. How to do it... 1. Let's start by defining a class and giving it a welcome method: Ext.define('Simple.Class', { welcome: function() { alert('Welcome to the app'); } }); 2. We provide Ext.override with the original class and add new functions: Ext.override(Simple.Class, { goodBye: function() { alert('Goodbye'); }, runAll: function() { this.welcome(); this.goodBye(); } }); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 1 41 3. Next, instantiate our class and call the new runAll() method: var app = new Simple.Class(); app.runAll(); // Welcome to the app // Goodbye 4. The override can also be written like this: Simple.Class.override({ //New members... }); How it works... The override method of Ext.Base takes the original class and loops around the new functions that you've created by adding them to the prototype of the existing class and replacing any existing ones with the new definitions. There's more... There are a number of other features in the framework that help you override and perform similar tasks. Ext.Base.callParent If you are looking to extend the behavior of an existing function, you can now easily call the original function passing any required arguments using the callParent method. Let's take the example from the recipe Extending Ext JS Components. The recipe shows how to add information text under a specified text field. We can amend that example and force our information text to appear on all text fields throughout the application with a very simple override. Ext.define('Cookbook.overrides.TextField', { override: 'Ext.form.field.Text', onRender: function(){ this.callParent(arguments); Ext.core.DomHelper.append(this.el, '
' + this.infoText + '
'); } }); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Classes, Object-Oriented Principles and Structuring your Application 42 We use the usual Ext.define function and give our override a name; this can be any name we want. Instead of including an extend configuration like we normally would, we add the override option, which is a string representation of the class we want to apply the override to. Just like the Extending Ext JS Components recipe we override the text field's onRender function and want to call the parent's onRender method so the field is fully rendered before our code is executed. We do this by including this.callParent(arguments), which will execute the Ext.form.field.Text class' onRender function. If we wanted to skip the Ext.form.field.Text class' onRender function and execute its parent class' (that is, Ext.form.field.Base) onRender function, (if we were, for example, providing a complete customization of the text Field's rendering process) we do this by calling Ext.form.field.Text.superclass.onRender.apply(this, arguments). Now we define infoText in our text field's config options and it will display the field; Ext.application({ launch: function(){ Ext.create('Ext.container.Viewport', { layout: 'fit', items: [{ xtype: 'form', defaultType: 'textfield', items: [{ fieldLabel: 'Security Question', name: 'securityQuestion', allowBlank: false, infoText: 'You are required to write a security question for your account.' }, { fieldLabel: 'Security Answer', name: 'securityAnswer', allowBlank: false, infoText: 'Please provide the answer to your security }] }] }); } }); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 1 43 Ext.Base.borrow With Ext.Base.borrow you can borrow another class' members and add them directly to the prototype of your class. Ext.Base.implement Ext.Base.implement is similar to override, but will always replace members with the same name and not give you the ability to call the original method. Just like Ext.Base.override, it's intended for adding methods or properties to the prototype of a class. See also ff Creating custom classes using the new Ext JS class system, explains how the class system works and its structure. ff The recipe Handling session timeouts with TaskManager in Appendix, Ext JS 4 Cookbook-Exploring Further, is a further demonstration on overriding classes. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 2 Manipulating the Dom, Handling Events, and Making AJAX Requests In this chapter, we will cover: ff Selecting DOM elements ff Traversing the DOM ff Manipulating DOM elements ff Creating new DOM elements ff Handling events on elements and components ff Delegating event handling of child elements ff Simple animation of elements ff Custom animations ff Parsing, formatting, and manipulating dates ff Loading data through AJAX ff Encoding and decoding JSON date This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Manipulating the Dom, Handling Events, and Making AJAX Requests 46 Introduction This chapter will cover topics about working with the Document Object Model (DOM), selecting, creating, and manipulating elements. We'll look at how to add built-in animations to your elements and how to create custom animations. We'll walk through creating your first AJAX request and encoding/decoding the data either in JSON or HTML format. Other topics include, handling events, working with dates, detecting browser features, and evaluating object types/values. Selecting DOM elements When creating interactive and responsive web applications it's vital to be able to access DOM elements for manipulation and processing. Ext JS provides multiple methods of retrieving references to those DOM elements, which we will explore in this recipe. Ext JS wraps basic DOM elements up in a class called Ext.Element, which is what we generally deal with when retrieving DOM elements and manipulating them. It provides a large number of helpful methods to make life easy for us. How to do it... Imagine an HTML page, with the Ext JS 4 library loaded into it, containing the following HTML fragment:

Ext JS 4 Cookbook

Authors

  • Stuart Ashworth
  • Andrew Duncan

What's new in Ext JS 4?

  • Charting
  • Drawing
  • Data Package
  • Enhanced Grid
  • Powerful Theming
This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 2 47 We will now use the Ext.get() method to retrieve a reference to the div containing the book's title and alert it to the user: var bookTitleEl = Ext.get('book-title'); alert('Book Title ID: ' + bookTitleEl.id); How it works... The Ext.get() method accepts a single parameter that can be either a DOM element's ID, a DOM node, or an existing Ext.Element instance. The method returns an instance of Ext.Element, which wraps the underlying DOM node, giving it additional functionality for further manipulation. The Ext.Element's underlying DOM node can be accessed through the dom property of an Ext.Element instance. For example, varbookTitleDomNode = Ext. get('book-title').dom. When an ID is passed into this method, the framework uses the browser's document. getElementById method, and after retrieving the DOM node, it creates a wrapping Ext.Element instance and then caches it to make future retrievals faster. If a DOM node is given to the method, then the framework skips the initial step outlined above and simply wraps the DOM node in an Ext.Element instance and caches it. Finally, when an Ext.Element instance is passed to the method, it refreshes the element's dom property with the latest contents using the document.getElementById method. The Ext.get method only returns a single element, so when you want to deal with multiple matches consider using either the Ext.select or Ext.query methods, which we will describe further. There's more... There are two other noteworthy methods provided by the framework that makes much more advanced DOM node retrieval possible. Ext.select The Ext.select method allows us to retrieve a collection of DOM nodes based on CSS selectors. The returned object from this method is an instance of either the Ext.CompositeElement or Ext.CompositeElementLite class. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Manipulating the Dom, Handling Events, and Making AJAX Requests 48 These two classes contain a collection of DOM elements (all wrapped in Ext.Element instances) that were matched and allows us to perform any method available on all of the elements in the collection. Both classes support all the methods of the Ext.Element and Ext.fx.Anim classes. If, for example, we wanted to hide all the author
  • tags in our previous HTML snippet, we can do so by calling the Ext.select method passing in a CSS selector. We are then able to call the hide method, which is added to the Ext.CompositeElement class from the Ext.Element class. var authorsListItemEls = Ext.select('ul#authors li'); authorsListItemEls.hide(); The Ext.select method accepts two further parameters:The second parameter is a Boolean value that determines if each node is given its own unique Ext.Element instance. When true, an Ext.CompositeElement instance is returned, giving each selected element its own wrapping Ext.Element instance. When false, an Ext.CompositeElementLite instance is returned, which uses the shared flyweight object to wrap each node. The last parameter allows you to specify the root that the select will start from. This accepts either an ID or an Ext.Element object. It is also useful to note that an Ext.Element instance has its own select method which forces itself to be the root of the select and so will only look at elements below it in the DOM's hierarchy. Ext.query Ext.query, an alias for Ext.DomQuery.select, selects an array of raw DOM nodes based on the specified CSS/XPath selector. This method is ideal when you require fast performance and only need to deal with DOM nodes directly without the framework's wrapping class and functionality. The previous example can be rewritten using Ext.query and the console output seen in the following screenshot, showing an array of DOM node references: console.log(Ext.query('ul#authors li')); Similarly to the select method an Ext.Element instance has its own query method, which forces the query's root to be that element. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 2 49 The Ext JS 4 documentation has numerous examples of selector syntax that this method can accept. See also ff The next three recipes, which explain how to traverse, manipulate, and create DOM elements. Traversing the DOM It is important to be able to move around the DOM based on the current context and retrieve references to surrounding elements. In this recipe, we will discover how to use Ext JS to traverse the DOM and access elements based on the context of the current element we are working with. Getting ready We will use the HTML snippet from the previous recipe, Selecting DOM elements, to demonstrate how to traverse the DOM, so make sure it is handy! How to do it... We will first discuss how to access a DOM element's siblings. 1. First we retrieve the Ext.Element instance that will be the root of our traversal. In this case we will use the Data Package item in the "What's new in Ext JS 4" list, simply because it is in the middle of the list. We do this using the item method which returns the item at the specified position in our returned collection: var dataPackageEl = Ext.select('ul#whats-new li').item(2); 2. Get the previous list item ('Drawing') using the prev method: var drawingEl = dataPackageEl.prev(); alert(drawingEl.dom.innerHTML); // alerts 'Drawing' 3. Get the next list item ('Enhanced Grid') using the next method: var enhancedGridEl = dataPackageEl.next(); alert(enhancedGridEl.dom.innerHTML); // alerts 'Enhanced Grid' This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Manipulating the Dom, Handling Events, and Making AJAX Requests 50 4. It is also possible to get the first and last child of an Ext.Element using the first and last methods respectively: var whatsNewEl = Ext.get('whats-new'); var chartingEl = whatsNewEl.first(); alert(chartingEl.dom.innerHTML); // alerts 'Charting' var owerfulThemingEl = whatsNewEl.last(); alert(powerfulThemingEl.dom.innerHTML); // alerts 'Powerful Theming' Each of the methods described can be passed a selector string in order to be more specific about the element returned. For example, using el.next('.my-class') will return the next element with the my-class CSS class. How it works... Each of the methods described use the Ext.Element's matchNode method to navigate the DOM until it finds the relevant element that is being asked for. Both the next and prev methods described only retrieve elements that are siblings of the root element, that is moving sideways in the DOM hierarchy rather than up or down). The first and last methods deal with the first level of children contained in the root element. There's more... In addition to traversing siblings (that is, accessing elements on the same level within the DOM hierarchy) Ext JS offers ways of moving up and down the tree gaining access to parents and children of an element. Direct parents and children We can demonstrate this by retrieving the parent
      element of one list element using the parent method of the Ext.Element class: var dataPackageEl = Ext.select('ul#whats-new li').item(2); alert(dataPackageEl.parent().id); // alerts 'whats-new' We are also able to move back down the tree by using the child() method, which returns the first element that matches the specified selector, as follows: var whatsNewEl = Ext.get('whats-new'); // get the first child LI element var firstListItemChildEl = whatsNewEl.child('li'); alert(firstListItemChildEl.dom.innerHTML); // alerts 'Charting' This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 2 51 The child method only returns direct children of the root element and so won't go deeper than one level. Multiple level traversal It is often necessary to traverse the DOM without restricting the number of levels we pass through in order to find a match. Fortunately, Ext JS provides us with the up and down methods, which don't impose such restrictions The up method moves up the tree from the current Ext.Element until it finds an element matching the specified selector, which it then returns. If it fails to find a match it will return null. We can also specify the maximum depth the traversal will go to, as the method's second parameter, in terms of a number or as a specific Element that we don't want to go past. The down method works in a very similar way but moves down the hierarchy to any depth until it finds a matching element without the option to restrict it. See also ff The previous recipe which discusses how to retrieve references to specific DOM elements. ff To find out about manipulating DOM elements see the next recipe which explains this in detail. ff To learn about creating your own DOM elements on the fly, go to the Creating new DOM elements recipe later in this chapter. Manipulating DOM elements So far we have discussed selecting elements and traversing through them. We will now explore how to manipulate those elements once we have got our hands on them. We will start by changing the style of an element by updating its inline styles and then by adding CSS classes. Following this we will explore how to show and hide elements. Getting ready Once again we will use the HTML snippet defined in the Selecting DOM elements recipe. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Manipulating the Dom, Handling Events, and Making AJAX Requests 52 How to do it... 1. Firstly, we will make the book's title bigger, color it red, and give it a bottom border by updating those specific styles using the setStyle method. 2. Retrieve a reference to the element, in this case the book's title heading: var bookTitleEl = Ext.get('book-title'); 3. Update the element's font-size style by itself. bookTitleEl.setStyle('font-size', '1.5em'); 4. Make the heading red and give it a bottom border at the same time: bookTitleEl.setStyle({ color: 'red', borderBottom: '3px solid red' }); 5. Finally, we will change the styling of the heading by adding a new CSS class to its element that will center the book's title. Define our new CSS class in the head of our HTML document: 6. Add the class to our book title element using the addCls method: bookTitleEl.addClass('book-title'); You can remove the class again by using the removeCls method, passing it the name of the CSS class that you would like to remove. How it works... The setStyle method can either accept two parameters, acting as a name/value pair, or a single parameter with multiple styles in a configuration object format. In step 2, we make the change by specifying the CSS style we want to update and the new value we want to give it. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 2 53 Step 3 makes use of the alternative syntax where a configuration object containing multiple name/value pairs is passed to the method, which loops through them, updating them each in turn. Notice that we specified the border-bottom CSS style as a camel- cased property name (borderBottom). This is because JavaScript doesn't allow hyphens in property names and so Ext JS converts this for us using the normalize method of Ext.Element. Under the surface, the framework is simply accessing the DOM's style collection and updating the individual styles. The following code snippet is taken directly from the framework and shows the styles being updated: me.dom.style[ELEMENT.normalize(style)] = value; Finally, the addCls method updates the DOM node's className property with the specified class name. Alternatively, the method accepts an array of class names and will apply all of these to the element. After executing the code in this recipe, you can inspect the HTML from FireBug (or Developer Tools) and you will see that the styles and classes have been applied. The following screenshot shows the HTML after this code has been executed: There's more... There are a huge number of further possibilities for manipulating DOM elements, unfortunately, too many to discuss in this recipe. However, we will go over a few of the most popular methods and how to use them. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Manipulating the Dom, Handling Events, and Making AJAX Requests 54 Showing and hiding an element The following code snippet will hide the book's title and then, after three seconds, show it again with a simple animation: bookTitleEl.hide(); setTimeout(function(){ bookTitleEl.show(true) }, 3000); // execute after 3000 ms We pass true to the show method to indicate that we want it to animate the transition. This could also be a proper animation config object, allowing you to customize the transition. The visibility mode of an Ext.Element instance becomes important when hiding it. This setting determines whether the CSS style properties—visibility or display are used or if an offset value is used to hide it. By passing Ext.Element.VISIBILTY to the setVisibilityMode method of an Ext.Element instance, the element is hidden but retains its space in the document. Using Ext.Element.DISPLAY will mean that the element does not retain any space and the document's elements will rearrange to suit. Finally, the Ext.Element.OFFSETS will move the element off-screen so it isn't visible. Updating the contents of an element The Ext.Element class provides us with a handy update method, which updates the element's innerHTML property with the new HTML we pass it: bookTitleEl.update('How to Make AWESOME Web Apps'); See also ff The previous two recipes discussing selecting DOM elements and traversing them. ff The Chapter 3, Laying Out Your Components, which discusses how to create your own DOM elements. ff To read more about the animation options that could have been used with the hide method discussed in the There's More... section, see the two animation recipes, later in this chapter. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 2 55 Creating new DOM elements After working through examples for selecting, traversing, and manipulating DOM elements, we will now move onto creating new ones and injecting them into our pages. Initially, we will demonstrate how to create a simple list item element with some basic configuration. We will then move onto exploring how to control the position of our new element. Getting ready As with the previous DOM recipes, we will use the same HTML snippet defined in the first recipe of this chapter. How to do it... In this recipe, we are going to add a new item to the What's new in Ext JS 4? list: 1. First, we define the configuration of our new element using a simple JavaScript object: var newClassSystemConfig = { tag: 'li', html: 'New Class System' }; 2. Next, we get a reference to the element we want to insert our new element into. In this case our What's New list: var whatsNewListEl = Ext.get('whats-new'); 3. Finally, we use the Ext.core.DomHelper class to create a new element, based on our configuration (in the newClassSystemConfig variable), and append it to the list: var newClassSystemEl = Ext.core.DomHelper.append(whatsNewListEl, newClassSystemConfig); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Manipulating the Dom, Handling Events, and Making AJAX Requests 56 4. By inspecting the HTML with our browser's developer tools, we can see the newly inserted HTML (highlighted in the following screenshot): How it works... The Ext.core.DomHelper class provides us with various methods to make creating and inserting new elements very quick and easy. Inside the framework, the append method calls the class' createDom method, which parses our configuration object and builds a DOM node to match the specification. It is then inserted into the page as the last child of the element passed. In this case our What's New list element. The createDom method parses all the properties of the configuration object as attributes of the newly created DOM element except for four reserved properties, which are: ff tag: this is used to define the element's tag (for example, div) ff children (or cn): this is used to define an array of sub elements defined in the same way ff cls: this will be mapped to the element's class attribute ff html: the content to be assigned to the inner HTML of the element The append method can also accept a plain HTML string, instead of a configuration object, which will be inserted into the document in exactly the same way. For example: var newClassSystemEl = Ext.core.DomHelper.append(whatsNewListEl, '
    • New Class System
    • '); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 2 57 There's more... The framework gives us full control over where new elements are inserted. By utilizing other methods of the Ext.core.DomHelper class, we can create new elements anywhere on our page. It also provides a way for leveraging the power of the Ext.Template class by compiling element configurations into reusable templates. Inserting a new element before or after an existing element The insertBefore method of Ext.core.DomHelper allows us to insert a new element before an existing element, as a sibling to it. For example, we can insert a new What's New list item at the top of the list (that is, before the first item): var whatsNewListEl = Ext.get('whats-new'); Ext.core.DomHelper.insertBefore(whatsNewListEl.first(), { tag: 'li', html: 'Infinite Scrolling' }); The insertAfter method can be used in exactly the same way to insert the new element after the one passed in as the first parameter. Using templates to insert elements Templates allow us to create HTML strings that contain data placeholders. These templates can be merged with a data object giving us an HTML string with its placeholders replaced with the values from the data object. The Ext.Template class can be used to append (or insertBefore/insertAfter) the output of this merge process to an existing element. We can use Ext.core. DomHelper'screateTemplate method to initially generate this template. We start by creating a template using the same configuration object syntax that we used earlier. Our template has one placeholder, named newFeature: var itemTpl = Ext.core.DomHelper.createTemplate({ tag: 'li', html: '{newfeature}' }); We then use the Ext.Template's append() method to insert the new element. The method accepts an ID or element as its first parameter to indicate which element the output should be appended to. The second parameter provides the data object to be applied to the template: itemTpl.append('whats-new', {newFeature: 'Row Editor'}); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Manipulating the Dom, Handling Events, and Making AJAX Requests 58 See also ff The previous three recipes, which go into further details about selecting, traversing, and manipulating DOM elements. ff The recipes in Chapter 4, UI Building Blocks—Trees, Panels, and Data Views, explaining templates in more detail. Handling events on elements and components Ext JS is an event driven framework and so it is important to be able to listen for raised events and react to them to control the flow of your application. These events could be raised through user interaction, for example, a button being clicked or keyboard keys being pressed, or internally within the framework, for example a store being loaded with data or a component being hidden. In this recipe, we will explain how to listen for components' events and execute code when they are raised. We will start off by listening for a simple click event on an element to explain the syntax and composition of an event handler. We will then move on to alternative ways of defining listeners. Getting ready We will set up a simple HTML page, which references the library and contains a single element within the tag as shown as follows:
      Ext JS 4 Cookbook
      How to do it... 1. Firstly, inside the Ext.onReady function, we retrieve a reference to the element (in the form of an Ext.Element instance). See the Selecting DOM Elements recipe, for more details: var el = Ext.get('my-div'); 2. We then attach an event handler function to the click event of the element using the on method. In our example, the function will show an alert when the event is raised: el.on('click', function(e, target, options){ alert('The Element was clicked!'); alert(this.id); }, this); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 2 59 Notice the output of the alert(this.id); line. The scope, defined by this being passed in as the third parameter, is the browser's window. Try changing this parameter to el and see what the alert displays. How it works... The Ext.Element's on method is an alias of the addListener method, and comes from the mixed in Ext.util.Observable class. This method tells the element that whenever the element's click event (defined by the first parameter) is raised, execute the function that is supplied as the second parameter. This can be either an anonymous function or a reference to a previously defined function. The third parameter indicates what scope the handling function will execute in (that is, what the this keyword will refer to). See the Scoping your functions recipe, in Chapter 1. There's more... It is possible to attach handlers to multiple events at once and to also define these handlers when configuring components at the start of their lifecycle. We will now demonstrate how to achieve this with two short examples. Defining multiple event handlers at once The on method also accepts an alternative parameter set that allows multiple event handlers to be assigned at once. By specifying a JavaScript object as the first parameter, with name/value pairs specifying the event name and its handling function, they will all be assigned at once. By defining a scope property within this object the handler functions will all be executed within this scope. el.on({ click: function(e, target, options){ alert('The Element was clicked!); alert(this.id); }, contextmenu: function(e, target, options){ alert('The Element was right-clicked!'); alert(this.id); }, scope: this }); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Manipulating the Dom, Handling Events, and Making AJAX Requests 60 Defining event handlers in config objects Ext JS components (for example, grids, panels, stores, and so on) all allow event handlers to be defined when they are configured using the listeners config option. Event handlers are defined in this config option in an identical fashion to the method described earlier. In the following short example, we create a simple Ext.panel.Panel and bind an event listener to its afterrender event, showing an alert: Ext.create('Ext.panel.Panel', { title: 'Ext JS 4 Cookbook', html: 'An Example Panel!', renderTo: Ext.getBody(), width: 500, listeners: { afterrender: function(){ alert('The Panel is rendered!'); }, scope: this } }); See also ff The first recipe of this chapter explaining how to select DOM elements. ff The Scoping your functions recipe in Chapter 1, Classes, Object-Oriented Principles and Structuring your Application. Delegating event handling of child elements Event handlers are a common cause of memory leaks and can cause performance degradation when not managed carefully. The more event handlers we create the more likely we are to introduce such problems, so we should try to avoid creating huge numbers of handlers when we don't have to. Event delegation is a technique where a single event handler is created on a parent element, which leverages the fact that the browser will bubble any events raised on one of its children to this parent element. If the target of the original event matches the delegate's selector then it will execute the event handler, otherwise nothing will happen. This means that instead of attaching an event handler to each individual child element, we only have to create a single handler on the parent element and then, within the handler, query which child element was actually clicked, and react appropriately. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 2 61 In this recipe, we will discover how to make use of event delegation when attaching event handlers to the items within a list. We will listen for click events on each of the list items and alert their innerHTML contents when they are clicked. Getting ready Once again we will make use of the HTML snippet from the first recipe of this chapter. How to do it... 1. Retrieve a reference to the What's New list element: var whatsNewEl = Ext.get('whats-new'); 2. Attach an event handler to the list's click event and specify we want to delegate this event to the list items (that is LI tags), by passing a configuration object to the on method's fourth argument, containing the delegate property: whatsNewEl.on('click', function(e, target, options){ alert(target.innerHTML); }, this, { delegate: 'li' }); When you run this code and click each of the list items you will see an alert with each of the items' contents. How it works... When attaching event handlers the framework builds up a dynamic function based on the configuration options passed in. In our example, only the delegate option has been used, but you can also specify additional options such as: ff - stopEvent ff - preventDefault ff - stopPropagation ff - delay ff - buffer When a delegate is specified, the dynamic function contains a simple call to the Ext.EventObject's getTarget method passing in the contents of the delegate property. If this call returns a value then we know that the event has occurred on a valid element and so it is fired, passing in this target element into the handler as the second argument. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Manipulating the Dom, Handling Events, and Making AJAX Requests 62 In our example, the dynamically generated function contains the following code: // output of the Ext.EventManager.createListenerWrap() method if (!Ext) { return; } e = Ext.EventObject.setEvent(e); var t = e.getTarget("li", this); if (!t) { return; } fn.call(scope || dom, e, t, options); As you can see, the highlighted line shows the delegate being used and the function only actually calling the event's handler function (fn) if a match was made. We could achieve identical functionality if we omitted the delegate option and included the getTarget call in our own handler function. An equivalent event handler is shown in the following code block: whatsNewEl.on('click', function(e, target, options){ var t = e.getTarget("li", this); if (!t) { return; } alert(target.innerHTML); }, this); There are couple of obvious advantages of using this technique over attaching an event handler to each element: ff We create fewer event handlers which means less memory is used and fewer opportunities exist for memory to leak ff If you were to add new child elements, the event handler is already set up to react to events on this new element There's more... When using event delegation we often need to perform different actions depending on which of the child elements the event is raised upon. For example, if we had the following toolbar, we would want to execute a different function when each of the links were clicked: This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 2 63
      Add | Edit |
      Firstly, we decorate each of the links with a class to distinguish it from the others. This will be used when deciding which of the links was actually clicked: We then add our click event handler to the toolbar div and delegate it with an a tag selector: toolbarEl.on('click', function(e, target, options){ }, this, { delegate: 'a' }); We use the getTarget method of the Ext.EventObject parameter to decide which of the child elements the event originated from. We do this by passing a selector to it which will return a matching element if it was found in the chain of elements involved in the event. When the getTarget call returns an element (which evaluates to true in an IF statement) we can then call the correct method for that link. toolbarEl.on('click', function(e, target, options){ if(e.getTarget('a.add')){ addItem(); } else if(e.getTarget('a.edit')){ editItem(); } else if(e.getTarget('a.delete')){ deleteItem(); } }, this, { delegate: 'a' }); See also ff The previous recipe titled, Handling events on elements and components. ff The recipe in Chapter 1, called Scoping your functions. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Manipulating the Dom, Handling Events, and Making AJAX Requests 64 Simple animation of elements Animating elements can be achieved easily with Ext JS. This recipe will demonstrate adding simple animations and transitions to an element or Ext JS component. How to do it... 1. Start by adding an element to the body of your HTML:
      2. Now style the DIV to help us see the animations working: 3. Inside your Ext.onReady function get the element: var el = Ext.get('animate'); 4. The first animation to try is puff. This effect expands the element in all directions while fading it out. Call the puff method passing in some FX options: el.puff({ easing: 'easeOut', duration: 1000, useDisplay: false }); 5. Having seen the box 'puff' we can substitute the method for one of the many other pre-defined animations. Remove or comment-out el.puff(): /* el.puff({ easing: 'easeOut', duration: 1000, useDisplay: false }); */ This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 2 65 6. Add the following to make your element switch off. This will collapse your element into itself, in the same way a TV might: el.switchOff({ easing: 'easeIn', duration: 2000, remove: false, useDisplay: false }); 7. Having tried both puff and switchOff now try any of the following (remembering to comment out the animations you are not trying to use): // slides the element into view from the direction specified // t = top, b = bottom, l = left, r = right el.slideIn('t', { easing: 'easeOut', duration: 500 }); // slides the element out of view in the direction specified el.slideOut('t', { easing: 'easeOut', duration: 500, remove: false, useDisplay: false }); // pulses a gray border (first parameter) 10 times (second parameter) el.frame("#444", 10, { duration: 1000 }); // fades the element out el.fadeOut({ opacity: 0, easing: 'easeOut', duration: 2000, remove: false, useDisplay: false }); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Manipulating the Dom, Handling Events, and Making AJAX Requests 66 8. To see animations working on Ext JS components add an animation to an alert: var messageBox = Ext.Msg.alert('Alert', 'This Message Box has an animation'); messageBox.getEl().frame("red", 3, { duration: 500 }); How it works... Ext.fx.Anim is the class that manages the animation of the element. It's made available to us through the use of Ext.Element. Ext.Element has a number of methods that can be used for animating the element as we've seen in the examples. Depending on the method you are calling you can either animate it by passing true to the animate parameter or by passing it an object literal with animation options. When the animation is called on the element, Ext JS, in essence, goes through a process of updating aspects of the element (for example, it's CSS) which alters its appearance on the browser. Each step of the process changes the element slightly to give the user the impression of a smooth transition. There are a number of animation options that can be set to alter the behavior of your animation. The common config options we set were: ff useDisplay: Boolean: when this is set to false the element will be hidden using the hide method after the animation is complete, otherwise it will be hidden using setDisplayed(false), which uses the CSS display property. By setting this to true the element will not take up any space in the document after it is hidden. ff duration: Number: this is the time (in milliseconds) that the animation will last. ff easing: String: the easing of the animation is a description of how the animation should calculate the intermediate values for the process. This gives you the ability to alter how the animation changes speed during the animation. We can set this with values such as, backIn, easeIn, easeOut, bounceOut, or elasticeIn. See also ff The four recipes at the beginning of this chapter explaining DOM retrieval and manipulating. ff The next recipe, which goes into more detail about creating custom animations. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 2 67 Custom animations Ext JS 4 introduces a brand new animation builder which allows us to build keyframe animations in a similar style to those available with CSS3. In this recipe, we will create a simple keyframe animation using the Ext.fx.Animator class. We will then move on to discuss how to have your application react to these custom animations by harnessing the built-in events and callbacks. Keyframe animation is when the animator defines the characteristics (position, size, color, and so on) of the elements involved in the animation at specific points in time. The software (in this case the Ext JS framework) then uses these key points to calculate the characteristics of elements in the frames between the two keyframes. This creates a smooth transition between the two (or more) key frames and gives us a nice animation. The example we will create in this recipe is that of a simple bouncing ball, which will change color after each bounce. How to do it... 1. We start by creating an element to represent our ball and giving it some basic styles:
      2. We next create a new instance of Ext.fx.Animator and target the new ball DIV as the element we want to animate: Ext.create('Ext.fx.Animator', { target: 'ball' }); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Manipulating the Dom, Handling Events, and Making AJAX Requests 68 3. We can now start defining our animation's duration and first and last keyframes. These two keyframes will define the start and end point of the animation: Ext.create('Ext.fx.Animator', { target: 'ball', duration: 5000, keyframes: { 0: { y: 50 }, 100: { y: 300 } } }); 4. Finally, we add the intermediate keyframes to produce the bouncing effect and color changes: Ext.create('Ext.fx.Animator', { target: 'ball', duration: 5000, // 5 seconds keyframes: { 0: { y: 50 }, 20: { y: 300 }, 40: { y: 175, backgroundColor: '#0000FF' }, 60: { y: 300 }, 80: { y: 275, backgroundColor: '#00FF00' }, 100: { y: 300 } } }); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 2 69 How it works... We initially tell our Ext.fx.Animator instance that the element we want to animate is the element with an ID of ball using the target config option. We also define the duration, in milliseconds, that our animation will run for. Our keyframes configuration takes a JavaScript object defining properties based on the percentage of time elapsed and a configuration object defining the element's characteristics at that point. For example, our 0 property tells the animator that after 0 percent of the 5 second duration (that is, at the very beginning) the element should have a y value of 50. The 100 property tells it that after 100 percent of our 5 second animation (that is, at the end) it should have a y value of 300. It is mandatory to define a 0 and 100 keyframe, so the animation has an explicit start and end point. Each of the intermediate keyframes tell the animator what the element should look like after that amount of time (for example, 20 => 20% of 5 second animation = 1 second). If a characteristic has a different value from the previous keyframes, then the animator will animate the transition between the two, be it moving it from position x to position y or changing color from red to blue. Internally, the framework builds up a collection of Ext.fx.Anim instances based on these keyframe definitions and the animation's other properties (duration, easing, and so on) and executes them one after another creating this smooth animation. There's more... There are several other options and events available to customize our animations even further, and integrate them into our application's process. We will explore a few of these options further. easing The animation's easing config option allows us to define how the animator calculates the frames between the defined keyframes. This gives us control over the speed in which the transition occurs, for example, whether it speeds up in the middle or if it bounces back once reaching its desired position. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Manipulating the Dom, Handling Events, and Making AJAX Requests 70 iterations This config option allows you to have the animation repeat itself this number of times. beforeanimate and afteranimate events These two events allow you to execute some code before the animation starts and after it has completed. This allows us to perform application logic once an animation has taken place, for example, after animating the removal of an Ext.view.View item, we could make an AJAX call to the server removing the item from our database using this event. keyframe event The keyframe event fires before performing the animation between two keyframes. It passes in a reference to the Ext.fx.Animator itself and the current keyframe's index as its first and second parameters respectively. See also ff The previous recipe, which covered simple animations on HTML elements. ff To read about how to listen for the beforeanimate and afteranimate events see the recipe called Handling events on elements and components. Parsing, formatting, and manipulating dates Dates crop up in every application in some form or another. Ext JS 4 provides a useful Ext.Date class that enhances the JavaScript Date object's functionality with a series of useful methods to help when working with dates. If you already have experience with PHP, you will be pleased to know that the formatting syntax for Ext.Date is a (comprehensive) subset of those available in PHP's date function. How to do it... 1. Start by instantiating the Date object, passing in numbers to represent the year, month, day, hour, and minute: var date = new Date(2011, 6, 6, 22, 30); 2. Add the following date/time patterns for formatting dates: Ext.Date.patterns = { ISO8601Long: "Y-m-d H:i:sP", This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 2 71 ISO8601Short: "Y-m-d", ShortDate: "n/j/y", FullDateTime: "l, F d, Y g:i:s A", LongTime: "g:i:s A", SortableDateTime: "Y-m-d\\TH:i:s", UniversalSortableDateTime: "Y-m-d H:i:sO" }; 3. With our defined patterns we can format the date and view our browser's console output: // 2011-07-06 22:30:00+01:00 console.log(Ext.Date.format(date, Ext.Date.patterns.ISO8601Long)); //2011-07-06 console.log(Ext.Date.format(date, Ext.Date.patterns. ISO8601Short)); //7/6/11 console.log(Ext.Date.format(date, Ext.Date.patterns.ShortDate)); //Wednesday, July 06, 2011 10:30:00 PM console.log(Ext.Date.format(date, Ext.Date.patterns. FullDateTime)); //10:30:00 PM console.log(Ext.Date.format(date, Ext.Date.patterns.LongTime)); //2011-07-06T22:30:00 console.log(Ext.Date.format(date, Ext.Date.patterns. SortableDateTime)); //2011-07-06 22:30:00+0100 console.log(Ext.Date.format(date, Ext.Date.patterns. UniversalSortableDateTime)); //Wednesday, the 6th of July 2011 10:30:00 PM console.log(Ext.Date.format(date, 'l, \\t\\he jS \\of F Y h:i:s A')); 4. Parse the following strings as dates with the Ext.Date.parse() method: //Mon Mar 07 2011 00:00:00 GMT+0000 (GMT Standard Time) console.log(Ext.Date.parse("3", 'n')); //Mon May 17 2010 00:00:00 GMT+0100 (GMT Daylight Time) console.log(Ext.Date.parse("2010-05-17", "Y-m-d")); //output: null as the date is invalid console.log(Ext.Date.parse("2011-11-31", "Y-m-d", true)); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Manipulating the Dom, Handling Events, and Making AJAX Requests 72 5. Ext JS adds further functionality for working with dates: //true console.log(Ext.Date.between(new Date('07/01/2011'), new Date('05/01/2011'), new Date('09/01/2011'))); //Thu Sep 30 2010 00:00:00 GMT+0100 (GMT Daylight Time) console.log(Ext.Date.add(new Date('09/30/2011'), Ext.Date.YEAR, -1)); //Sun Jul 31 2011 00:00:00 GMT+0100 (GMT Daylight Time) console.log(Ext.Date.getLastDateOfMonth(new Date('07/01/2011'))); // true console.log(Ext.Date.isDST(new Date('07/01/2011'))); //false console.log(Ext.Date.isValid(2011, 29, 2)); How it works... The Ext.Date.patterns object we defined in step 2 is our working set of recognized formats for our application. These aren't provided in the framework, however, they can be found on the Ext.Date documentation page for copying and editing. Ext.Date in Ext JS 4 is a series of static methods that are written specifically to manipulate and work with a JavaScript Date instance. Ext.Date.parse takes three arguments: ff input: string ff format: string ff strict: boolean (optional) The purpose of Ext.Date.parse is to take a string input and return it as a Date object. We need to specify a format to allow the parser to ensure the date it returns is what we expect. Ext JS follows the same formatting syntax as PHP. So, if the input is 3 and the format is n (numeric representation of a month, without leading zeros), then the parser will interpret this as March. If the input were 3 and the format m (numeric representation of a month, with leading zeros) and strict were true, then parsing would fail as our input string would require the leading zero to be valid. The complete list of formats can be found in the Ext.Date documentation. There's more... A useful property in Ext.Date is defaultFormat. When used with Ext.util.Format. dateRenderer and Ext.util.Format.date, dates will appear in a format you specify. Setting a default format helps by ensuring your application displays dates in a format suited to the locale, for example, Ext.Date.defaultFormat = 'd/m/Y';. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 2 73 Ext.Date has many other features that haven't been looked at in our examples, such as: ff getDayOfYear(Date date) is used for getting the numeric day of the year. ff getDaysInMonth(Date date) is used for getting the number of days in a given month. ff getElapsed(Date dateA, [Date dateB]) is used for finding out the number of milliseconds between two dates. If you don't include the dateB parameter, it will default to the current date. ff getGMTOffset(Date date, [Boolean colon]) is used for getting the GMT offset of a given date (for example, +02:00). If you exclude the colon parameter the output would be +0200. ff getTimezone(Date date) will return the abbreviated timezone name. See also ff The recipe Loading and Parsing Dates into a Date field in Chapter 6, Using and Configuring Form Fields. Loading data with AJAX In this recipe, we will discover how to load data asynchronously using the Ext.Ajax class. We will demonstrate how to use the Ext.Ajax.request method for loading data and how to process the XMLHttpRequest response that's returned. Additionally, we will learn how to handle errors. Getting ready Make sure you have a web server installed and running on your development computer. For the purposes of this demonstration your web server will need to serve JSON files. If you run into problems you may need to add a MIME type for JSON (application/json). How to do it... 1. Create a file called ajaxRequest.json and add some JSON: { "id": 1, "firstname": "John", "lastname": "Smith" } This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Manipulating the Dom, Handling Events, and Making AJAX Requests 74 2. Create an Ajax request with Ext JS by adding the following inside the Ext.onReady function: Ext.Ajax.request({ url: 'ajaxRequest.json', success: function(response, options){ console.log('The Success function was called.'); console.log(response.responseText); }, failure: function(response, options){ console.log('The Failure function was called.'); var statusCode = response.status; var statusText = response.statusText; console.log(statusCode + ' (' + statusText + ')'); }, callback: function(options, success, response){ console.log('The Callback function was called.'); console.log('Successful Request? ' + success); }, timeout: 60000 //60 seconds (default is 30) }); How it works... Ext.Ajax is a singleton and a subclass of Ext.data.Connection that creates, opens, and sends the request using the XMLHttpRequest object in JavaScript. As a result, the request is made asynchronously. Processing the response is done using the callback property and the success/failure properties. In each of these the response parameter is the XMLHttpRequest objects response data. The data returned by the server is available in the responseText property. There's more... When making AJAX requests there are a number of other features in Ext JS for posting JSON or XML, caching, and cross-domain requests. POST JSON or XML data Defining the params config of an Ext.Ajax.request allows you to add parameters to a request. However, if you wish to POST JSON or XML, simply define either xmlData (object) or jsonData (object/string) properties in your Ext.Ajax.request. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 2 75 If you define xmlData or jsonData and params at the same time all params will be appended to the URL. Disabling client-side caching of Ajax requests Adding disableCaching: true to an Ext.Ajax.request will add a unique parameter to the URL for GET requests. This can be of particular use for ensuring the client is always receiving up-to-date data. Use Ext.data.JsonP for cross-domain If you are looking to make cross-domain requests, you'll need to use JSONP. Ext JS provides an Ext.data.JsonP class for this purpose with similar configuration to Ext.Ajax. Here's a quick example: Ext.data.JsonP.request({ url: 'http://www.example.com/api/example', params: { apiKey: '1234' }, callbackKey: 'myCallbackFn', success: function(){ //task on successful request }, failure: function(){ //task on failed request }, scope: this }); See also ff The next recipe, for learning how to encode and decode JSON data. ff Chapter 7, Working with the Ext JS Data Package, explores the Ext JS 4 data package in further depth and demonstrates Models, Proxies, and Stores. Encoding and decoding JSON data JavaScript Object Notation (or JSON) is a lightweight data interchange format that's human readable and very useful for representing simple data structures and data objects. As JSON is language independent, it's ideal for use with frameworks such as Ext JS. Ext JS has an excellent range of functions that work with JSON. However, let's start at the beginning with encoding and decoding data in JSON format. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Manipulating the Dom, Handling Events, and Making AJAX Requests 76 A useful tool when working with JSON is the online JSON validator JSON Lint, which validates and formats JSON. This is available at www.jsonlint.com. How to do it... 1. Start by creating a JavaScript object. We're going to encode this object as JSON: var objToEncode = { foo: "bar", id: 1, today: new Date(), isJson: true, size: ["Small", "Medium", "Large"]," obj: { item: "My Item" } }; 2. Encode the object as JSON using Ext.encode: console.log(encodedJson); /* { "foo": "bar", "id": 1, "today": "2011-07-09T15:01:21", "isJson": true, "size": [ "Small", "Medium", "Large" ], "obj": { "item": "My Item" } } */ This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 2 77 3. Decode the JSON back to an object using Ext.decode(): var decodedJson = Ext.decode(encodedJson); console.log(decodedJson.foo); //bar console.log(decodedJson.id); //1 console.log(decodedJson.size[0]); //Small console.log(decodedJson.obj.item); //My Item How it works... To demonstrate encoding and decoding JSON data we started by creating an object with a variety of data types. The encoding and decoding is done by the Ext.JSON class. However, we've used the shorthand methods Ext.encode and Ext.decode. Encoding is done by the doEncode method, which determines what it's to be encoded to (for example, string, array, and so on) and returns the item as a string in JSON format. Decoding is done by the doDecode method, which evaluates the JSON using the eval method: doDecode = function(json) {     return eval("(" + json + ')'); }, There's more... The Ext.JSON class provides another useful method for encoding dates—encodeDate. This method returns a date as a JSON string in the following format: yyyy-mm-ddThh:mm:ss. It's easy to override the default date by setting your own, for example: Ext.JSON.encodeDate = function(d) {   return d.format('"d/m/Y"'); }; See also ff The next recipe as it demonstrates encoding/decoding HTML. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 3 Laying Out Your Components In this chapter, we will cover: ff Using a FitLayout to expand components to fill their parent ff Creating flexible vertical layouts with VBoxes ff Creating flexible horizontal layouts with HBoxes ff Displaying content in columns ff Collapsible layouts with accordions ff Displaying stacked components with the CardLayout ff Anchor components to their parent's dimensions ff Creating fullscreen applications with the BorderLayout ff Combining multiple layouts Introduction This chapter explores the layout system in Ext JS 4 and demonstrates how to use these layouts to place your user interface components. The layouts that we will be working with are FitLayout, BorderLayout, HBox layout, VBox layout, ColumnLayout, TableLayout, AccoridionLayout, CardLayout, AnchorLayout, and AbsoluteLayout. The final recipe will combine a number of these layouts to create a framework for a rich desktop-style application. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Laying Out Your Components 80 Using a FitLayout to expand components to fill their parent The FitLayout makes it possible for you to expand a component to fill its parent container. The FitLayout is easy to use and requires no configuration. The screenshot shows how a FitLayout can be used to automatically expand a panel to take up its parent's entire area: How to do it... 1. Start by creating a simple Ext.panel.Panel and render it to the document's body. This panel will be the parent item that contains our inner, expanded-to-fit component: Ext.create('Ext.panel.Panel', { title: 'Fit Layout', width: 500, height: 200, renderTo: Ext.getBody() }); 2. Now, add the inner panel by adding it to the parent's items collection: Ext.create('Ext.panel.Panel', { title: 'Fit Layout', width: 500, height: 200, items: { title: 'Inner Panel', html: 'Panel content', bodyPadding: 10, border: true }, renderTo: Ext.getBody() }); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 3 81 3. Finally, apply the fit layout to the parent panel to have the inner panel expand: Ext.create('Ext.panel.Panel', { title: 'Fit Layout', width: 500, height: 200, layout: 'fit', items: { title: 'Inner Panel', html: 'Panel content', bodyPadding: 10, border: true }, renderTo: Ext.getBody() }); The difference between step two and three can be seen by observing the border. How it works... The FitLayout works by defining the layout config option as fit in the parent container. This tells Ext JS that the child item should expand to fill the entire space available from its parent. In the example, the parent panel has a fixed height and width. When layout: 'fit' is set, the child panel automatically expands to fill the space within the parent component. There's more... It's worth noting that the FitLayout will only work for the first child item of the parent container. If you have multiple items defined, the first will be displayed (as it will expand into the remaining space of its parent) and the others will not be visible. See also ff The recipe demonstrating the CardLayout shows how the FitLayout can be extended. ff Displaying a simple form in a window recipe in Chapter 4, UI Building Blocks—Trees, Panels, and Data Views, is another example of the FitLayout in action. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Laying Out Your Components 82 Creating flexible vertical layouts with VBoxes The VBoxLayout allows you to align components vertically down a container. The following screenshot shows three panels vertically aligned, dividing the available space between them: How to do it... 1. Start by creating a Viewport: Ext.create('Ext.container.Viewport', {}); 2. Define a layout for the Viewport with the following configuration: Ext.create('Ext.container.Viewport', { layout: { type: 'vbox', align: 'stretch', padding: 10 } }); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 3 83 3. Finally add panels to the items collection and give them a height or flex configuration: Ext.create('Ext.container.Viewport', { layout: { type: 'vbox', align: 'stretch', animate: true, //{ duration: 2000, easing: 'easeIn' }, padding: 10 }, items: [{ xtype: 'panel', title: 'Panel One', height: 100 }, { xtype: 'panel', title: 'Panel Two', flex: 1 }, { xtype: 'panel', title: 'Panel Three', frame: true, flex: 3 }] }); How it works... The Viewport automatically consumes all available space in the browser. We've added a layout object to the Viewport with the type defined as a vbox. The Vbox layout automatically arranges child items vertically within their parent. The align config option in this case is set to stretch. This means that all child items will be stretched horizontally to fill the width of the parent container (in this case the full width of the browser window). The padding config option adds padding to all child items in the example. A value of 10 adds padding of 10px to the parent container giving a small space between the parent and its children. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Laying Out Your Components 84 Finally, and most importantly, the height or flex is set for each child. In this example, panel one has a fixed height of 100px. No matter how the height is defined for the other children, panel one will always be 100px high. Panel two and panel three have a flex value defined. The flex config option relatively flexes the items vertically in the container. Panel two has flex: 1 and Panel three has flex: 3, therefore, 25 percent of the remaining parent space (remember we've already used 100px with panel one) is given to panel two and 75 percent of the space is given to panel three. Flex values are calculated by ((Container Height – Fixed Height of Child Components) / Sum of Flexes) * Flex Value (assume parent is 1000px high). For example, ((1000 – 100) / (1 + 3)) * 1 (or 3). There's more... The VBox layout has some useful configuration options that are described as follows: align: String The align config option controls how child items are horizontally aligned in a VBox layout. Valid values are: ff left: This is the default value. All items in the VBox layout will be horizontally aligned to the left of the container and will use their width config to define how wide they are. ff center: All items will be horizontally aligned to the middle (or center) of the container. ff stretch: Each item will be horizontally stretched to fit the width of the container. ff stretchmax: This horizontally stretches all items to the width of the largest item creating a uniform look without having to individually define a width for each item. pack: String The pack config option controls how the child items are packed together. If the items do not stretch to the full height of the parent container (that is, have no flex values), it's possible to align them to the top, middle, or bottom using this option. Valid values are: ff start: This is the default value. It will align all items to the top of the parent container ff center: Center aligns all items to the middle (or center) of the container ff end: Aligns all items to the bottom of the container This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 3 85 See also ff The next recipe demonstrating the HBoxLayout. Creating flexible horizontal layouts with HBoxes The HBox layout allows you to align components horizontally across a container. The screenshot shows three panels horizontally aligned, dividing the available space between them: How to do it... 1. Start by creating a Viewport: Ext.create('Ext.container.Viewport', {}); 2. Define an HBox layout in the Viewport with the following configuration: Ext.create('Ext.container.Viewport', { layout: { type: 'hbox', align: 'stretchmax', pack: 'center' } }); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Laying Out Your Components 86 3. Add three panels to the items collection, the first and third with fixed widths, and the second with a flex of 1 to take up the remaining browser space: Ext.create('Ext.container.Viewport', { layout: { type: 'hbox', align: 'stretchmax', padding: 10 }, items: [{ xtype: 'panel', title: 'Panel One', height: 200, width: 100 }, { xtype: 'panel', title: 'Panel Two', flex: 1 }, { xtype: 'panel', title: 'Panel Three', width: 100 }] }); How it works... The Viewport automatically consumes the space available in the browser window. Defining an HBox layout ensures that Ext JS horizontally positions each child item giving the feeling of columns in this example. The align configuration is stretchmax meaning that all child items will automatically be stretched to the height of the tallest child. Panel one's height is defined as 200. With align: 'stretchmax', panel two and three will also be stretched to 200px high. Panel one and panel three both have their width defined as 100. These panels now have a set width of 100px even when the user resizes their browser. They are not fluid. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 3 87 Panel two, on the other hand, is set to flex with flex: 1. The HBox layout will calculate a width for panel two and will update this value when the user resizes the browser. Full details on how flex widths are calculated is provided in the recipe demonstrating the VBox layout. There's more... The HBox layout has some useful configuration options that are described as follows. These options are the same as the Vbox layout. However, they work on a horizontal layout. align: String The align config option controls how child items are vertically aligned in an HBox Layout. Valid values are shown as follows: ff top: This is the default value. All items in the HBox layout will be vertically aligned to the top of the container. ff middle: All items will be vertically aligned to the middle (or center) of the container. ff stretch: Each item will be vertically stretched to fit the height of the container. ff stretchmax: This vertically stretches all items to the height of the largest item creating a uniform look without having to individually define a height for each item. pack: String The pack config option controls how the child items are packed together. If the items do not stretch to the full width of the parent container, it's possible to align them to the left, middle, or right using this option. Valid values are as follows: ff start: This is the default value. It will align all items to the left of the parent container. ff center: This aligns all items to the middle of the container. ff end: It aligns all items to the right of the container. See also ff The previous recipe on VBoxLayout. This recipe also explains in further detail how flex works. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Laying Out Your Components 88 Displaying content in columns The ColumnLayout is used for creating multi-column layouts. The width of each column can be specified as fixed (in pixels), as a percentage, or a mixture of both. This recipe will demonstrate creating a three column layout with one fixed width column and two columns with widths specified as percentages: How to do it... 1. Start by creating a panel rendered to the document's body: Ext.create('Ext.Panel', { title: 'Column Layout', width: 500, height: 200, renderTo: Ext.getBody() }); 2. Add three child panels to the parent's items collection: Ext.create('Ext.Panel', { title: 'Column Layout', width: 500, height: 200, items: [{ title: 'Panel One', html: 'Panel One Content' }, { This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 3 89 title: 'Panel Two', html: 'Panel Two Content' }, { title: 'Panel Three', html: 'Panel Three Content' }], renderTo: Ext.getBody() }); 3. Give the parent a ColumnLayout configuration and define column widths for each child: Ext.create('Ext.Panel', { title: 'Column Layout', width: 500, height: 200, layout: 'column', items: [{ title: 'Panel One', columnWidth: .2, html: 'Panel One Content' }, { title: 'Panel Two', columnWidth: .8, html: 'Panel Two Content' }, { title: 'Panel Three', width: 100, html: 'Panel Three Content' }], renderTo: Ext.getBody() }); 4. The layout can be enhanced by giving each panel a frame and defining a fixed height for the child items: Ext.create('Ext.Panel', { title: 'Column Layout', width: 500, height: 200, layout: 'column', frame: true, defaults: { height: 165, frame: true }, This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Laying Out Your Components 90 items: [{ title: 'Panel One', columnWidth: .2, html: 'Panel One Content' }, { title: 'Panel Two', columnWidth: .8, margin: '0 5 0 5', html: 'Panel Two Content' }, { title: 'Panel Three', width: 100, html: 'Panel Three Content' }], renderTo: Ext.getBody() }); How it works... Steps 3 and 4 are where the magic happens. The parent panel's layout config is set to column. Ext JS now looks for either the width or columnWidth config option for child panels. A fixed width is set on column three (width: 100). The framework now calculates the space remaining and assigns it to the first and second columns. The columnWidth property is a percentage (although represented as a decimal) and must total 1 (100 percent) across the items. Panel one is 20 percent (defined as .2) and Panel two is 80 percent (defined as .8). Ext JS can now calculate the pixel widths for panel one and two and assign them accordingly. In step 4, we further enhance the layout by adding frame: true (adds a frame to the panel) to the parent and defaults config object. The default option is an object of config options that will be applied to the child items (not the parent panel). This is where the fixed height for each column is set and a frame added to the child panels. Finally, the margin config option in panel two adds a 5px margin to the left and right of the component. The margin could have been defined as a number; however, this numeric value would apply the same margin to all four sides of the component. See also ff The HBox layout is an alternative method of creating columns. See the previous recipe for more information. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 3 91 Collapsible layouts with accordions Accordion layouts allow us to present multiple containers in a fashion where only one of the containers is expanded and visible at any one time. The other containers are collapsed, leaving only their headers visible, each of which can be clicked on to expand that content area and collapse the others. This layout style is useful when only one area of content is required to be displayed at once. This might be because there isn't enough space to display everything at once or only one area is relevant at a time. In this recipe, we will describe how to create an accordion layout to display some simple information about Sencha products. A screenshot of our final goal can be seen as follows: How to do it... 1. Once again we start by creating a simple Ext.panel.Panel and render it to the document's body. This panel will be the parent item that contains each of our accordion items: Ext.create('Ext.panel.Panel', { title: 'Accordion Layout', width: 350, height: 400, renderTo: Ext.getBody() }); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Laying Out Your Components 92 2. Next, we create the four panels that will make up the accordion structure and add them to the parent panel's items collection: Ext.create('Ext.panel.Panel', { title: 'Accordion Layout', width: 350, height: 400, renderTo: Ext.getBody(), items: [{ title: 'Ext JS 4', html: 'Ext JS 4 ...' // full content omitted }, { title: 'Sencha Touch', html: 'Sencha Touch ...' }, { title: 'Ext Designer', html: 'Ext Designer ...' }, { title: 'Sencha Animator', html: 'With Sencha Animator...' }] }); 3. Note that we use the title option to give our accordion items a header bar that provides a clickable area to expand and collapse the content pane. 4. After creating this structure, our panel looks like the following screenshot. By default, it has adopted an Auto layout and the panels flow one after the other. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 3 93 5. Finally, we apply the accordion layout to the parent panel and the framework takes care of the rest! It is simply a case of setting the layout configuration option to 'accordion': Ext.create('Ext.panel.Panel', { title: 'Accordion Layout', width: 350, height: 450, layout: 'accordion', renderTo: Ext.getBody(), items: [{ title: 'Ext JS 4', html: 'Ext JS 4 ...' }, { title: 'Sencha Touch', html: 'Sencha Touch ...' }, { title: 'Ext Designer', html: 'Ext Designer ...' }, { title: 'Sencha Animator', html: 'With Sencha Animator ...' }] }); How it works... By setting an Ext.panel.Panel's layout to accordion, the framework automatically adds a +/- button to each child item's header and makes it clickable allowing the item to be expanded and collapsed. It also ensures that only one panel's content is visible at a time by collapsing the open panel when another is clicked. We can also set the layout's titleCollapse configuration option to true, to make a click anywhere on the entire title bar collapse or expand, that accordion item. See also ff The recipe covering the VBox layout, which the accordion layout extends from. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Laying Out Your Components 94 Displaying stacked components with CardLayouts Ext JS gives us the ability to stack components on top of one another allowing us to only show a single item at once and have the option to switch between them as we wish. There are several possible use cases for this type of layout, for example, a wizard style form, a content carousel, or a tabbed layout. The Ext.tab.TabPanel component is in fact based on the card layout and uses it to manage the tabs' layout. In this recipe, we will demonstrate how to use the Ext.layout.container.Card layout manager to create a simple account creation wizard as shown in the following screenshot: How to do it... 1. We start by creating our wrapping Ext.panel.Panel which will contain each of our cards: var panel = Ext.create('Ext.panel.Panel', { title: 'Account Creation Wizard - Card Layout', width: 350, height: 300, renderTo: Ext.getBody() }); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 3 95 2. We now create our three cards that will form each screen of our wizard. The first contains three form fields to gather the user's first and last names and an e-mail address: var card1 = new Ext.panel.Panel({ bodyStyle: 'padding: 20px', title: 'Personal Info', items: [{ xtype: 'textfield', fieldLabel: 'First Name' }, { xtype: 'textfield', fieldLabel: 'Last Name' }, { xtype: 'textfield', fieldLabel: 'Email Address', vtype: 'email' }] }); 3. The second card contains two fields for the user's username and password: var card2 = new Ext.panel.Panel({ bodyStyle: 'padding: 20px', title: 'Account Info', items: [{ xtype: 'textfield', fieldLabel: 'Username' }, { xtype: 'textfield', fieldLabel: 'Password', inputType: 'password' }] }); 4. Finally, we create a third card with a simple success message: var card3 = new Ext.panel.Panel({ bodyStyle: 'padding: 20px', title: 'Account Creation Successful!', html: 'Success!' }); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Laying Out Your Components 96 5. Now that we have our three cards defined we can add them to the wrapper panel we created earlier within its items array: var panel = Ext.create('Ext.panel.Panel', { title: 'Account Creation Wizard - Card Layout', width: 350, height: 300, renderTo: Ext.getBody(), items: [card1, card2, card3] }); 6. If we looked at our progress so far in the browser, we will see the panels flow one after the other in the same way as the accordion example. To turn this into a CardLayout, we simply add the layout config option and assign it a value of 'card' and it will transform the child panels, making them fill the parent and hide all except the first one: var panel = Ext.create('Ext.panel.Panel', { title: 'Account Creation Wizard - Card Layout', width: 350, height: 300, layout: 'card', renderTo: Ext.getBody(), items: [card1, card2, card3] }); 7. After applying the Ext.layout.container.Card class to the parent panel we can see that the child panels are stacked on top of each other. We will now add some navigation buttons so that the user can move between the three cards as you would in a wizard. We do this by creating a toolbar and two buttons which, when pressed, tell the layout to move to the next or previous card, if one exists: var panel = Ext.create('Ext.panel.Panel', { title: 'Account Creation Wizard - Card Layout', width: 350, height: 300, layout: 'card', renderTo: Ext.getBody(), items: [card1, card2, card3], bbar: ['->', { xtype: 'button', text: 'Previous', handler: function(btn){ var layout = panel.getLayout(); if (layout.getPrev()) { layout.prev(); } } }, { xtype: 'button', This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 3 97 text: 'Next', handler: function(btn){ var layout = panel.getLayout(); if (layout.getNext()) { layout.next(); } } }] }); How it works... The Ext.layout.container.Card layout extends the Ext.layout.container.Fit layout and so forces each of the parent panel's children to fill its available space. The layout then hides all but one of these children, giving the effect of a stack. If you inspect our small example with your developer tools, you will see that each of the three cards have exactly the same dimensions but the second and third are set to "display: none": By calling the next, prev, or setActiveItem methods, the layout switches which of the cards is visible and hides the rest. There's more... The Card layout gives us the option to defer the rendering of its cards until they are activated. This is extremely useful when the cards have large amounts of content or lots of components within them as it means the browser isn't required to deal with laying out and rendering markup that is not immediately visible. This will give us a performance boost when dealing with such content-heavy card layouts. To use this feature we simply set the deferredRender option to true within the layoutConfig configuration object. This config option is only applicable to card layouts and the components that use them (for example, tab panels). This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Laying Out Your Components 98 See also ff The first recipe in this chapter that explores the FitLayout, which this layout extends from. ff The Creating a tabbed layout recipe in the next chapter which uses the Ext.tab.Panel component that is built upon the card layout. Anchor components to their parent's dimensions We can use the Ext.layout.container.Anchor class to size a child component relative to the dimensions of its parent. This class inherits from the Ext.layout.container. Container layout which means that by default a component that has the Anchor layout will have its children flow vertically within it one after another. The Anchor layout gives us four options for defining the size of a child component. The first, and most popular, is a simple percentage value which is used to calculate the child's width and height based on the parent's dimensions (or the defined anchorSize property, see There's more... for further details). The second option is a basic offset value that will size the child to the parent's full width (or height) minus the offset value, with the component anchored to the parent's left edge. The third variation allows the child to be sized based on its static width and height and its parent's width and height by providing a value of bottom or right. Alternatively, a shorthand value of b or r can be used. This alternative can only be used when the child component has a fixed size or the parent component has an anchorSize defined. Finally, we are able to specify a combination of percentages and offsets to give us maximum control over the layout. During this recipe, we are going to walk through creating an anchor layout that makes use of these variations and demonstrates how to create a simple fluid layout. How to do it... We will create each of our example panels within an Ext.window.Window component, which is a subclass of the Ext.panel.Panel class that we have used in the previous recipe. By doing this, we can easily resize them to see the effect of our anchor configuration in real time. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 3 99 1. We first create a panel anchored using percentages to its parent (an instance of Ext.window.Window). We apply the Anchor layout using the layout config option and configure the panel with the anchor property, passing it a string containing two percentages, in this case 100% and 35%, representing the width and height. Note that the width value comes first and the height second: var win = Ext.create('Ext.window.Window', { x: 0, y: 0, width: 400, height: 400, title: 'Anchor Layout Panel - Percentages', layout: 'anchor', items: [{ xtype: 'panel', title: 'Percentages', html: 'Panel Content', anchor: '100% 35%' }] }); win.show(); 2. Next, we create a panel anchored using offsets. This time we give the anchor configuration option a string value containing two numbers, -150 and -100: var win = Ext.create('Ext.window.Window', { x: 500, y: 0, width: 400, height: 400, title: 'Anchor Layout Panel - Offsets', layout: 'anchor', items: [{ xtype: 'panel', title: 'Offsets', This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Laying Out Your Components 100 html: 'Panel Content', anchor: '-150 -100' }] }); win.show(); 3. The use of the Sides variation can be demonstrated using the following code, where the child Panel is given a fixed height and width and the anchor option defined as r b: var win = Ext.create('Ext.window.Window', { x: 0, y: 500, width: 400, height: 400, title: 'Anchor Layout Panel - Sides', layout: 'anchor', items: [{ xtype: 'panel', title: 'Sides', height: 200, width: 200, html: 'Panel Content', This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 3 101 anchor: 'r b' }] }); win.show(); 4. Finally, we can anchor a panel using a combination of offsets and percentages. We do this in exactly the same manner as the previous examples, separating the width and height of anchor values with a space: var win = Ext.create('Ext.window.Window', { x: 500, y: 500, width: 400, height: 400, title: 'Anchor Layout Panel - Combination', layout: 'anchor', items: [{ xtype: 'panel', title: 'Combination', html: 'Panel Content', anchor: '75% -150' }] }); win.show(); How it works... This layout works by parsing the defined anchor values and using them to calculate the child's dimensions based on the parent container's sizes. The first anchor value specifies the component's width and the second its height. These calculated dimensions are then applied to the component. In Step 1, we use percentage values. These are used to calculate the final dimensions in the following way: Width = 100% of Parent's Width (400px) = 400px Height = 35% of Parent's Height (400px) = 140px Step 2 shows our example use of offsets to calculate its dimensions, as follows: Width = Parent's Width (400px) - 150px = 250px Height = Parent's Height (400px) - 100px = 300px This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Laying Out Your Components 102 In Step 3, the anchor config is given a value of r b. This is used in conjunction with the child components fixed dimensions and sizes them based on the difference between the parent and child's starting width subtracted from the parent container's actual width. This is shown in the following formula: Width = Parent's actual width - (Parent's defined width – Child's defined width) Height = Parent's actual height - (Parent's defined height – Child's defined height) There's more... Rather than having the parent's actual dimensions determining the children components size we can give the parent an additional configuration option called anchorSize. At present, this property will take the place of the parent's actual width and height when the layout calculates the relative sizes of its children. The anchorSize configuration can be specified in two ways: ff The first way is: anchorSize: 200 // if a single number is specified it defaults to the component's width ff And the second way is: anchorSize: { width: 200, height: 200 } // width and height explicitly set See also ff See the next recipe that outlines how to use the Absolute layout to position components precisely within their container. ff The first recipe, in Chapter 5, Loading, Submitting and Validating Forms discusses constructing forms, which use the Anchor layout as their default. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 3 103 Creating fullscreen applications with the BorderLayout If you are looking to create a desktop style experience with your user interface then the BorderLayout is for you. The BorderLayout is very much an application-oriented layout, supporting multiple nested panels, the ability to collapse regions by clicking on the regions' header or collapse icon, and the resizing of regions by clicking-and-dragging the splitter bar between them. This recipe will demonstrate a simple BorderLayout using the maximum number of regions configurable (north, south, east, west, and center). The west and east regions will be collapsible, with the east region loading pre-collapsed. Resizing will be demonstrated on the south and west regions. These four borders will surround the center region, which regardless of your configuration is required for a BorderLayout to work: This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Laying Out Your Components 104 How to do it... 1. Start by creating a Viewport: Ext.create('Ext.container.Viewport', {}); 2. Set the layout to border and add a center region to the items collection: Ext.create('Ext.container.Viewport', { layout: 'border', items: [{ title: 'Center', region: 'center' }] }); A requirement of the BorderLayout is that it has a child item with a center region . 3. Next, add four further regions (north, south, east, and west) to the items collection. Specify a height, width, or flex for these regions: Ext.create('Ext.container.Viewport', { layout: 'border', items: [{ region: 'north', height: 100, xtype: 'container' }, { title: 'West', region: 'west', flex: .3 }, { title: 'Center', region: 'center' }, { title: 'East', region: 'east', width: 200 }, { title: 'South', region: 'south', flex: .3 }] }); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 3 105 4. The north and south regions can have a height or flex value to calculate height whereas west and east regions can have a width or flex value to calculate width. 5. Spread the regions apart with a 5px margin. Ext.create('Ext.container.Viewport', { layout: 'border', items: [{ region: 'north', margins: 5, height: 100, xtype: 'container' }, { title: 'West', region: 'west', margins: '0 5 0 5', flex: .3 }, { title: 'Center', region: 'center' }, { title: 'East', region: 'east', margins: '0 5 0 5', width: 200 }, { title: 'South', region: 'south', margins: '0 5 5 5', flex: .3 }] }); 6. Additional functionality, such as resizing and collapsing regions, can be added in the following way: Ext.create('Ext.container.Viewport', { layout: 'border', items: [{ region: 'north', margins: 5, height: 100, }, { title: 'West', region: 'west', margins: '0 5 0 5', This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Laying Out Your Components 106 flex: .3, collapsible: true, split: true, titleCollapse: true }, { title: 'Center', region: 'center' }, { title: 'East', region: 'east', margins: '0 5 0 5', width: 200, collapsible: true, collapsed: true }, { title: 'South', region: 'south', margins: '0 5 5 5', flex: .3, split: true }] }); How it works... The BorderLayout, as the name suggests, creates a layout of components that borders a center component. Therefore, a requirement of the BorderLayout is that one item must be specified as the center. A Viewport renders itself to the document's body and automatically consumes the viewable area. The center region, which you must include for a BorderLayout to work, automatically expands to consume the empty space left over from the other regions in your layout. It does this by having a pre-defined flex value of 1 for both height and width. The north and south regions take a height or flex configuration. In this example, north has a fixed height of 100px and south a flex of 3. The south and center's heights are calculated based on the height remaining in the browser window. In this example, the height of south is just under a third of the height of the center. The west and east regions, instead, take a width or flex configuration. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 3 107 We add further functionality with collapsed: true, collapsible: true, split: true, and titleCollapse: true specified in the desired regions' configuration. They do the following: ff collapsed: true means the region will start collapsed (the regions need to be Ext.panel.Panel to be collapsible) ff collapsible: true allows the user to expand/collapse the panel by clicking on the toggle tool that's added to the header ff titleCollapse: true makes the panel collapse no matter where the user clicks on the panel's header ff split: true makes the region resizable by allowing the users to click-and-drag the dividing bar between regions See also ff The next recipe, which expands on what we have covered here. Combining multiple layouts This chapter has demonstrated how to use individual layouts with Ext JS, however, it's time to bring everything together, combining some of the layouts to create the beginning of a desktop style Ext JS application. This recipe will start with an Ext.Viewport to form the basis of a single page web application because it is a component that always expands to fill the browser window. We will then look at using a BorderLayout and combining it with: ff An AccordionLayout in the west region as a main menu ff A CardLayout in the center region ff A combination layout with the HBoxLayout and VBoxLayout, also in the center panel This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Laying Out Your Components 108 ff An Ext.tab.Panel to navigate between screens: How to do it... 1. Start by specifying the panel for the west region. In this case, the west region will be a main menu: var mainMenu = Ext.create('Ext.panel.Panel', { title: 'Main Menu', region: 'west', margins: '0 5 5 5', flex: .3, collapsible: true, titleCollapse: true, layout: 'accordion', layoutConfig: { animate: false, multi: true }, items: [{ title: 'Product Management' }, { This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 3 109 title: 'User Management' }, { title: 'Settings' }] }); 2. Next, add the panels that will make up the CreateUserWizard wizard. This CardLayout will eventually sit in the center region: var card1 = new Ext.panel.Panel({ bodyStyle: 'padding: 20px', title: 'Personal Info', border: false, items: [{ xtype: 'textfield', fieldLabel: 'First Name' }, { xtype: 'textfield', fieldLabel: 'Last Name' }, { xtype: 'textfield', fieldLabel: 'Email Address', vtype: 'email' }] }); var card2 = new Ext.panel.Panel({ bodyStyle: 'padding: 20px', title: 'Account Info', border: false, items: [{ xtype: 'textfield', fieldLabel: 'Username' }, { xtype: 'textfield', fieldLabel: 'Password', inputType: 'password' }] }); var card3 = new Ext.panel.Panel({ bodyStyle: 'padding: 20px', title: 'Account Creation Successful!', border: false, html: 'Success!' }); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Laying Out Your Components 110 var createUserWizard = Ext.create('Ext.panel.Panel', { title: 'Create User Wizard', layout: 'card', deferredRender: true, items: [card1, card2, card3], bbar: ['->', { xtype: 'button', text: 'Previous', handler: function(btn){ var layout = cardPanel.getLayout(); if (layout.getPrev()) { layout.prev(); } } }, { xtype: 'button', text: 'Next', handler: function(btn){ var layout = cardPanel.getLayout(); if (layout.getNext()) { layout.next(); } } }] }); 3. This step defines the User Management screen, which also sits in the center region: var userManagementPanel = Ext.create('Ext.panel.Panel', { title: 'User Management', layout: { type: 'hbox', align: 'stretch', padding: 10 }, defaults: { flex: 1 }, items: [{ xtype: 'container', margins: '0 5 0 0', layout: { This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 3 111 type: 'vbox', align: 'stretch', animate: true }, defaults: { flex: 1, frame: true }, items: [{ title: 'User Contact Information', margins: '0 0 5 0' }, { title: 'Session Log' }] }, { xtype: 'container', layout: { type: 'vbox', align: 'stretch', animate: true }, defaults: { flex: 1, frame: true }, items: [{ title: 'Account Privileges', margins: '0 0 5 0' }, { title: 'Purchase History', }] }] }); 4. Now, add the center region. We'll make the center region a basic Ext.tab.Panel with the CreateUserWizard and UserManagementPanel as separate tabs: var contentPanel = Ext.create('Ext.tab.Panel', { region: 'center', margins: '0 5 5 0', items: [createUserWizard, userManagementPanel] }); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Laying Out Your Components 112 5. Finally, bring everything together by creating an Ext.container.Viewport and specifying a BorderLayout for the viewport: Ext.create('Ext.container.Viewport', { layout: 'border', items: [{ region: 'north', margins: 5, height: 100, xtype: 'container', html: 'Logo Here' }, mainMenu, contentPanel] }); How it works... Ext JS allows us to nest all layout types within one another as deeply as we like. This allows us to create complex layouts. When the browser window, or a component that contains children, is resized; new items added or existing items removed, the framework will automatically recalculate each child layout recursively, until each level has been computed. This means that, once configured, the layouts will always remain up-to-date. This behavior is accomplished by executing the doLayout() method which triggers this recalculation process. We should never call this method manually within our application code as the framework calls it whenever it is required. See also ff See all of the previous recipes in this chapter for details of each layout used. ff The Constructing a complex form layout recipe in Chapter 5, Loading, Submitting, and Validating Forms, which makes use of a variety of layouts. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 4 UI Building Blocks— Trees, Panels, and Data Views In this chapter, we will cover: ff Loading a tree's nodes from the server ff Sorting Tree nodes ff Dragging-and-dropping nodes within a tree ff Using a tree as a menu to load content into another panel ff Docking items to a panel's edges ff Displaying a simple form in a window ff Creating a tabbed layout with tooltips ff Manipulating a tab panel's TabBar ff Executing inline JavaScript in an XTemplate to customize appearance ff Creating Ext.XTemplate member functions ff Adding logic to XTemplates ff Formatting dates within an XTemplate ff Creating a DataView and binding it to a data store ff Displaying a detailed window after clicking on a Data View node This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 UI Building Blocks—Trees, Panels, and Data Views 114 Introduction Creating and manipulating the basic components that Ext JS provides is fundamental to producing a rich application. In this chapter, we will cover three fundamental Ext JS UI components and explore how to configure and control them within your applications. We will start by exploring the Ext.tree.Panel class and demonstrate how to populate a tree with server-side data. From there we will delve into manipulating the tree component through filtering and sorting, and then by dragging-and-dropping individual nodes. Finally, we will show you how to incorporate a tree into a more real-life scenario by using it as a menu and loading another panel following node clicks. The Ext.panel.Panel class will be focused on next where we will discover how to configure its headers, dock items to its edges, and to create tab panels. We will then go on to look at ways in which these tab panels can be customized with things like tooltips, icons, and tab positions. Before moving onto the Ext.view.View component. We will talk about XTemplates, which allow us to create dynamic HTML very easily. We will discuss various features of the Ext.XTemplate class and how to use it. This will lead us nicely into the Ext.view.View class, which makes heavy use of XTemplates, and is used to bind a data store to a presentation generator. We will also look into handling events on a View and how to integrate custom plugins to enrich our users' experience. Loading a tree's nodes from the server Creating a tree in your user interface is achieved using a Tree Panel. This recipe gives you the knowledge required to create and configure a Tree Panel and load JSON data asynchronously from your server to the tree. The final tree will look like the following screenshot: This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 4 115 Getting ready Make sure you have a web server installed and running on your development computer. For the purposes of this demonstration, your web server will need to serve JSON files. If your server is not capable, add a MIME type for JSON (application/json). How to do it... 1. Start by defining an Ext.data.TreeStore to load our data into: var store = Ext.create('Ext.data.TreeStore', { proxy: { type: 'ajax', url: 'treeData.json' }, root: { text: 'Countries', expanded: true } }); 2. The treeData.json file that we are loading from contains a simple array of data, some of these objects contain nested data that will form our tree structure. A sample can be seen as follows: [{ "text": "United Kindom", "children": [{ "text": "Glasgow", "leaf": true }, { "text": "Edinburgh", "leaf": true }, { "text": "London", "leaf": true }], "leaf": false }, { "text": "France", "leaf": true } ... ] This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 UI Building Blocks—Trees, Panels, and Data Views 116 3. Create the Tree Panel and render it to the document's body. Ext.create('Ext.tree.Panel', { title: 'Countries & Cities', width: 500, height: 300, store: store, rootVisible: false, renderTo: Ext.getBody(), style: 'margin: 50px' }); How it works... The Ext.tree.Panel class holds the configuration required for the tree. The simple tree demonstrated in this recipe is bound to a TreeStore. All Tree Panel's must be bound to an Ext.data.TreeStore instance. The Ext.data.TreeStore is used to load nodes into the tree and by default, if no other Model is provided, uses an implicit Model that implements the Ext.data.NodeInterface class, which provides a range of methods for working with the data in your tree. This store has a proxy and a root defined. As we are working with server-side data the proxy's type is configured as ajax. The url is the location of the remote data (in this case treeData.json). The root contains the root node—Countries for this dataset. As we want the child data that's loaded to be visible immediately, we set expanded: true. We've also set rootVisible: false in the Tree Panel which hides Countries from view, but still shows its children. The data in our treeData.json file contains a JSON object for each tree node. The text property is displayed in the tree for each node. The leaf property indicates whether or not the node has any children. By setting this to false, the node will not be expandable. Finally, the children property contains an array of nodes, defined in the same way, which are displayed as children of the parent node. See also ff You may be interested in visiting Chapter 7, Working with the Ext JS Data Package, to learn more about the framework's data package. ff The next three recipes in this chapter, which explore the TreePanel in further detail. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 4 117 Sorting tree nodes Sorting the data that's asynchronously loaded to a tree is straight-forward with Ext JS. This recipe, demonstrates how to sort a tree's data on the client-side. How to do it... 1. Create a store with the sorters configuration option defined with an object specifying property and direction values: var store = Ext.create('Ext.data.TreeStore', { proxy: { type: 'ajax', url: 'treeData.json' }, root: { text: 'Countries', expanded: true }, sorters: [{ property: 'text', direction: 'ASC' //for descending change to 'DESC' }] }); 2. Create a tree to load the sorted data to. Ext.create('Ext.tree.Panel', { title: 'Countries', width: 500, height: 200, store: store, rootVisible: false, renderTo: Ext.getBody(), style: 'margin: 50px' }); How it works... The sorting in this example is carried out by an Ext.util.Sorter that's defined in the sorters property of the TreeStore. The Sorter requires either a property or sorterFn option. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 UI Building Blocks—Trees, Panels, and Data Views 118 The sorter is configured with a property, which is the name of the field that we would like to sort by. The direction property sets the sorting direction for the sorter. This will default to ASC (ascending) if it's omitted from the Sorter configuration. There's more... Sorting data on the client side can be enhanced further with: Complex and custom sorting It's possible to customize the sorter and make complex comparisons by removing the property configuration and replacing it with the sorterFn option and assigning it a function that performs our sorting comparisons. The arguments of sorterFn are the two objects being compared and it should return: ff -1 if objectOne is less than objectTwo ff 0 if objectOne is equal to objectTwo ff 1 if objectOne is greater than objectTwo A simple example of this can be written to sort our list of countries by the length of their name which isn't possible using a simple property sort. We start by defining our sorter function, which we will compare two countries names and output the correct value either -1, 0, or 1: var nameLengthSorter = function(objectOne, objectTwo){ var objectOneLength = objectOne.get('text').length, objectTwoLength= objectTwo.get('text').length; if(objectOneLength=== objectTwoLength){ return 0; } else if(objectOneLength', 'Docked toolbar at the top'] }], renderTo: Ext.getBody(), style: 'margin: 50px' }); When creating a toolbar, any component configuration added to the items array without an xtype will default to being a button. 3. Add another item to the dockedItems collection. This time dock it to the bottom of the panel: Ext.create('Ext.panel.Panel', { title: 'Panel Header', width: 500, height: 200, bodyPadding: 10, html: 'Panel Content', dockedItems: [{ xtype: 'toolbar', dock: 'top', items: [{ text: 'Click me' }, '->', 'Docked toolbar at the top'] }, { xtype: 'toolbar', dock: 'bottom', items: [{ This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 UI Building Blocks—Trees, Panels, and Data Views 128 xtype: 'button', text: 'Click me' }, '->', 'Docked toolbar at the bottom'] }], renderTo: Ext.getBody(), style: 'margin: 50px' }); The string -> in our items array is automatically converted into an instance of the Ext.toolbar.Fill class, which is a simple component with a flex of 1. This forces all the toolbar items after it to the right side of the toolbar. Other useful toolbar shortcuts that can be included in its items array are -, which will create an Ext.toolbar. Separator instance that displays a vertical separator line, and (a space), which equates to an Ext.toolbar. Spacer instance that adds a space between components. 4. Add a footer bar to the panel using the fbar config option. The footer bar appears beneath the panel: Ext.create('Ext.panel.Panel', { title: 'Panel Header', width: 500, height: 200, bodyPadding: 10, html: 'Panel Content', fbar: ['Docked toolbar at the bottom (on footer)', { xtype: 'button', text: 'Click Me' }], renderTo: Ext.getBody(), style: 'margin: 50px' }); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 4 129 5. Ext JS provides further shortcuts for adding items to the left, right, top, and bottom of a panel: Ext.create('Ext.panel.Panel', { title: 'Panel Header', width: 500, height: 200, bodyPadding: 10, html: 'Panel Content', lbar: ['lbar'], rbar: ['rbar'], tbar: ['tbar'], bbar: ['bbar'], renderTo: Ext.getBody(), style: 'margin: 50px' }); How it works... In steps 2 and 3 the toolbar is defined in the panel's dockedItems collection. We specify where the item will be docked using the dock config option which takes the following values: 'left', 'right', 'top', or 'bottom'. Step three demonstrates how it's possible to define multiple items in the dockedItems collection and specify a different position for each. The fbar config option highlighted in step 4 shows that it's possible to dock a toolbar to a panel's bottom edge conveniently, without having to specify it in the dockedItems collection. It creates a docked item for you that is docked to the bottom with the ui config option set to 'footer'. The final step shows four other convenient methods provided in the framework for quickly specifying docked items on all sides of a panel. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 UI Building Blocks—Trees, Panels, and Data Views 130 There's more… It's also possible to add dockeditems to your Panels at runtime, which means you can dynamically specify the dockedItems depending on specified criteria. var panel = Ext.create('Ext.panel.Panel', { title: 'Panel Header', width: 500, height: 200, bodyPadding: 10, html: 'Panel Content', renderTo: Ext.getBody() }); panel.addDocked({ dock: 'top', xtype: 'toolbar', items: [{ text: 'button' }] }); The addDocked method simply adds the docked item to the container. You must not forget to configure your component with the dock config option ('left', 'right', 'top' or 'bottom') to ensure the component is docked in the correct position. Displaying a simple form in a window Most websites and applications make use of windows and forms on a regular basis. Here you'll learn the basics for creating a simple form and window with Ext JS and combining the two to display the form to the user. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 4 131 How to do it... 1. Start by creating a simple form with two fields and a button. var form = Ext.create('Ext.form.Panel', { bodyPadding: 10, border: false, defaultType: 'textfield', items: [{ fieldLabel: 'Username', name: 'username', allowBlank: false }, { fieldLabel: 'Password', name: 'password', inputType: 'password', allowBlank: false }], buttons: [{ text: 'Login', formBind: true, disabled: true, handler: function(){ alert('Login Button Pressed'); } }] }); 2. Create an Ext.window.Window with the form in its items collection and display it to the user using the show method: Ext.create('Ext.window.Window', { title: 'Login Window', height: 140, width: 300, layout: 'fit', items: [form] }).show(); How it works... There are two main components in this recipe, the form, and the window. As you can see from the code above it's very straight-forward creating and configuring both. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 UI Building Blocks—Trees, Panels, and Data Views 132 This recipe doesn't demonstrate how to populate or submit the form. The next chapter holds all the information you require to make the form actually work. Firstly, we create an Ext.form.Panel. This will contain the configuration for the form. The form fields are specified in the panel's items collection. Setting defaultType: 'textfield' saves us from having to specify xtype: 'textfield' on each item. The benefits of defaultType are really seen when you have a large form with lots of similar fields. Although the Password field is a textfield remember to set inputType: 'password' to ensure the password isn't displayed. The buttons collection contains our form's buttons. In this example we've set formBind: true. This ensures the button remains disabled until the form is valid. Each field has allowBlank: false configured, making the field required before the form validates. Additionally, to ensure the button starts inactive disabled: true must be set on the button. Step two shows how to create a window with Ext JS. The form is added to the window's items collection. Finally, to display the window we call the show method. See also ff Forms and form fields are covered in greater depth over the next two chapters. The recipe Constructing a complex form layout in Chapter 5 may be particularly useful. Creating a tabbed layout with tooltips Creating a tabbed layout and adding tooltips to the tabs is straight-forward with Ext JS 4. In this recipe we will show you how to create a tab panel with multiple tabs, add tooltip text, and have the framework display the text as you hover over a tab. How to do it... 1. Initialize the global QuickTipManager instance. We need this for the tooltips to be displayed: Ext.tip.QuickTipManager.init(); 2. Create an Ext.tab.Panel with two tabs (added to the Panel's items collection) and render it to the document's body: Ext.create('Ext.tab.Panel', { This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 4 133 width: 500, height: 200, style: 'margin: 50px', renderTo: Ext.getBody(), items: [{ title: 'Tab One' }, { title: 'Tab Two' }] }); 3. In tab two add a tooltip to the tabConfig config option. tabConfig takes configuration options that are applied to the Panel's Ext.tab.Tab instance: Ext.create('Ext.tab.Panel', { width: 500, height: 200, style: 'margin: 50px', renderTo: Ext.getBody(), items: [{ title: 'Tab One' }, { title: 'Tab Two', tabConfig: { tooltip: 'Tab Two Tooltip Text' } }] }); 4. Customize the tooltip with configuration options from Ext.tip.QuickTip class: Ext.create('Ext.tab.Panel', { width: 500, height: 200, style: 'margin: 50px', renderTo: Ext.getBody(), items: [{ title: 'Tab One' }, { title: 'Tab Two', tabConfig: { tooltip: 'Tab Two Tooltip Text' } }, { title: 'Tab Three', tabConfig: { tooltip: { title: 'Tooltip Header', This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 UI Building Blocks—Trees, Panels, and Data Views 134 text: 'Tab Three Tooltip Text' } } }] }); How it works... Creating a tab panel in Ext JS is done using the Ext.tab.Panel class. Each tab is defined in the panel's items collection and has an associated Ext.tab.Tab instance created automatically, which represents the tab element that allows it to be activated. In step three, we add the tabConfig config option to the second tab. Inside the tabConfig object we can specify configuration for the Ext.tab.Tab class. From here we've added a tooltip with a string value. Step 4 shows how it's possible to customize a tooltip by assigning an object literal with configuration from the Ext.tip.QuickTip class. Don't forget to initialize the QuickTipManager instance otherwise your tooltips won't appear. This is done by calling Ext.tip.QuickTipManager.init(); at the start of your code (as seen in step one). See also ff The next recipe, which covers techniques on how to manipulate a TabBar. Manipulating a tab panel's TabBar A tab panel's TabBar is the area at the top of its content, which displays a button or tab for each of the child panels within it. By clicking on these tabs the relevant panel is displayed. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 4 135 These tabs can be extensively customized, and in this recipe, we will discuss how to achieve the following: ff Configure a tab with an icon ff Dynamically switch icons ff Set tabs' widths ff Change the position of the tab bar ff Show and hide tabs on the fly How to do it... To start with we will create a basic Ext.tab.Panel with three child panels. We will use this as the base for all our examples: var tabPanel = Ext.create('Ext.tab.Panel', { width: 500, height: 200, style: 'margin: 50px', renderTo: Ext.getBody(), items: [{ title: 'Tab One', html: 'This is Tab One' }, { title: 'Tab Two - has a very, very long and silly title', html: 'This is Tab Two' }, { title: 'Tab Three', html: 'This is Tab Three' }] }); Configure a tab with an icon We will now add an icon to the first tab by creating a CSS class, with the icon defined as a background image, and applying it to the tab by using the tabConfig configuration option: ... { title: 'Tab One', This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 UI Building Blocks—Trees, Panels, and Data Views 136 tabConfig: { cls: 'x-btn-text-icon', iconCls: 'icon-refresh' }, html: 'This is Tab One' } ... The result of this can be seen as follows: Dynamically switch icons Although we have specified this refresh icon at configuration time, we can also change it at runtime by using the setIconCls method of the Tab component. We can access a panel tab by using its tab property. We will demonstrate this by adding a button to the tab panel's tbar that switches the icon class to another. In the following code snippet tabPanelIcon contains the original tab panel's instance: tbar: [{ text: 'Switch Icon', handler: function(){ tabPanelIcon.items.get(0).tab.setIconCls('icon-tick'); } }] Set tabs' widths Ext JS gives us the option to control the minimum and maximum width of any tab in a tab panel's tab bar. We can do this by using the minTabWidth and maxTabWidth config options on the tab panel itself: ... minTabWidth: 100, maxTabWidth: 200 ... We are also able to specify the absolute width of individual tabs by defining the width option within the panel's tabConfig property: ... tabConfig: { width: 150 } ... This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 4 137 We can see in the screenshot that the first tab has its width constrained by the minTabWidth option. The second is constrained by the maxTabWidth value, which means some of its title is cut off. The third is set by the explicit value given. Change the position of the tab bar It is possible to reposition the tab bar to the bottom of the tab panel rather than having it at the top. This is very simple to accomplish by using the tabPosition config option of the Ext.tab.Panel class, and providing it with a value of bottom or top: ... tabPosition: 'bottom' ... Show and hide tabs on the fly One new feature of the Ext JS 4's tab panel is that we are able to show and hide tabs on the fly. We can do this simply by calling the show, hide, or setVisible method on the child panels' Tab component. We will demonstrate this by adding a button that toggles the visibility of the second tab: tbar: [{ text: 'Toggle Tab Two', handler: function(){ var tab = tabPanelVisibility.items.get(1).tab; tab.setVisible(!tab.isVisible()); } }] The second tab is hidden from view as shown in the following screenshot: This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 UI Building Blocks—Trees, Panels, and Data Views 138 How it works... When a panel is added to a tab panel it has an Ext.tab.Tab instance created to represent it in the tab bar. This class extends the Ext.button.Button class and gives us access to a selection of the properties and methods from this class. The Ext.tab.Tab's instance is accessible through the panel's tab property as we have seen in our examples. We can configure the Ext.tab.Tab by defining the tabConfig option in the panel's definition. This object is automatically applied to the Ext.tab.Tab instance when it is created and so through this, we can configure it how we need. Executing inline JavaScript to in an XTemplate customize appearance Within an Ext.XTemplate's HTML we are able to include small pieces of JavaScript that will be executed when the template is processed. This feature is extremely useful for adding things such as, conditional formatting, simple member manipulation, or formatting. In this recipe, we are going to demonstrate how to include some inline JavaScript within an Ext.XTemplate, in order to format our output dynamically based on the data's values. The example that we will use is a simple bug list. We will use inline JavaScript code to color each row based on the severity of the bug. How to do it... 1. We start by defining an array of bugs that we will use in our template (we have only shown one to demonstrate the structure): var bugData = [{ id: 1, title: 'Bug 1', description: 'Bug 1 Description', status: 'In Progress', severity: 1 } ... ] 2. Next, we create our template, containing a simple HTML table, to display our bug details. We then use overwrite method of the Ext.XTemplate to insert the generated HTML into the document's body element, replacing any HTML already there: var tpl = new Ext.XTemplate( This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 4 139 '', '', '', '', '', '', '', '', '', '', '', '', '', '
      TitleDescriptionSeverity
      {title}{description}{severity}
      '); tpl.overwrite(Ext.getBody(), bugData); 3. We now add a background style to the status cell and add some inline JavaScript to either color the background green when the status is complete or transparent when it isn't: var tpl = new Ext.XTemplate( '', '', '', '', '', '', '', '', '', '', '', '', '', '
      TitleDescriptionSeverity
      {title}{description}{severity}
      '); tpl.overwrite(Ext.getBody(), bugData); How it works... The Ext.XTemplate class evaluates anything within the tags {[...]} as JavaScript code and executes it within the scope of the template itself (see the next recipe, Creating Ext.XTemplate member functions, for more details). This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 UI Building Blocks—Trees, Panels, and Data Views 140 In our example we use a simple ternary if statement to decide what color to use depending on the status property's value. We access the current data object and its properties through the values keyword which references the current object. There's more... As well as the values object there are several other variables that can be used to access helpful bits of information within an XTemplate: ff parent: the current data object's parent object. For example, if we were looping through an array the parent variable would refer to the data object that contains the array as a property ff xindex: when inside a loop, this variable contains the current (1-based) index ff xcount: when inside a loop, this variable contains the total number of items in the array that is being iterated over See also ff The next recipe, to discover how to work with member functions in Ext.XTemplates. ff The recipe, Adding logic to Ext.XTemplates, for other methods of customizing the Ext.XTemplate. Creating Ext.XTemplate member functions Ext.XTemplates can be configured to contain functions and properties that are enclosed within the scope of the template itself. These functions can be used to encapsulate presentation logic, formatting, and other simple template data processing. We are going to demonstrate how these functions can be included in an XTemplate's definition and how to access them within our template's code. We will do this by building upon the example that we created in the previous recipe. Our goal is to highlight each of the rows that are deemed by our manager as high priority, in our situation this means any bug with a severity rating of 4 or 5. How to do it... 1. We begin by creating our sample data and our basic Ext.XTemplate in the same way as we did in the previous recipe: var bugData = [{ id: 1, title: 'Bug 1', This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 4 141 description: 'Bug 1 Description', status: 'In Progress', severity: 1 } ... ]; var tpl = new Ext.XTemplate( '', '', '', '', '', '', '', '', '', '', '', '', '', '
      TitleDescriptionSeverity
      {title}{description}{severity}
      '); tpl.overwrite(Ext.getBody(), bugData); 2. Next, we define a configuration object containing a simple function called isHighPriority, which accepts one argument called severity. We then include this object as the final parameter of our XTemplate's constructor call: var tpl = new Ext.XTemplate( '', '', '', '', '', '', '', '', '', '', '', '', '', '
      TitleDescriptionSeverity
      {title}{description}{severity}
      ', { This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 UI Building Blocks—Trees, Panels, and Data Views 142 isHighPriority: function(severity){ return severity > 3 } } ); tpl.overwrite(Ext.getBody(), bugData); 3. Finally, we use the tag's if attribute, to call our function and add some style markup if it evaluates to true. In this situation, we will add a red border and pink background if the bug is deemed to be of a high priority: var tpl = new Ext.XTemplate('', '
      ', '', '', '', '', '', '', 'style="background-color: pink; border: 2px solid #FF0000;">', '', '', '', '', '', '
      TitleDescriptionSeverity
      {title}{description}{severity}
      ', { isHighPriority: function(severity){ return severity > 3 } } ); tpl.overwrite(Ext.getBody(), bugData); 4. We are also able to call our member functions within the inline JavaScript that we spoke of in our last recipe, as those code snippets are executed in the scope of the XTemplate. Our example could be rewritten as: {[this.isHighPriority(values.severity) ? "style="background-color: pink; border: 2px solid #FF0000; " :""]} This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 4 143 How it works... If we pass an object to the constructor of Ext.XTemplate's (at any position) it is applied to the XTemplate's definition, in a similar manner to defining your own custom methods when extending a class. By applying these configuration options the methods can be executed anywhere in the template from within tags or in inline code blocks. Properties can also be defined in this configuration object. These can come in use when state is required to be preserved while a template is generated. There's more... Another valuable use of member functions is in formatting values before they are presented. If we were to consider using a member function to format the description of a bug, only the first ten characters would be displayed as shown in the following code: // function defined in the constructor’s config object formatDescription: function(description){ return description.substring(0, 10); } // code within your template {[this.formatDescription(values.description)]} This approach is perfectly valid and will function perfectly. However, Ext JS provides us with a clever short-hand syntax for achieving this goal: {description:this.formatDescription} When parsing this code, the XTemplate knows to execute the formatDescription method and pass the specified field (in this case the description) into it as its first parameter. If your function accepts additional parameters, such as a length to decide how many characters to show, they can be added to the code in the following way: // function defined in the constructor's config object formatDescription: function(description, numberOfChars){ return description.substring(0, numberOfChars); } // code within your template {description:this.formatDescription(10)} This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 UI Building Blocks—Trees, Panels, and Data Views 144 The field's value is always passed to the function as its first parameter, with the extra values following after it. Adding logic to Ext.XTemplates When presenting data, it is important to be able to include logic to allow different markup to be created depending on the data given to it. Ext JS' Ext.XTemplate provides that functionality in an easy to use way, which we shall explore in this recipe. We will show this functionality by adding an additional column to our bug table from our previous two recipes, to display the owner of the bug. If that owner is the same as the current user (which we will define as a member property) then we will display Me otherwise we will display the owner's name. Getting ready We will base this recipe heavily on the examples created in the previous two recipes. So make sure you have a look at them and remind yourself what we did! How to do it... 1. We will start by adding an owner property to each of our bug data objects and a currentUser property to our XTemplate's configuration object. var bugData = [{ title: 'Bug 1', description: 'Bug 1 Description', status: 'In Progress', severity: 1, owner: 'Bob' } ... ]; { // member property added to the XTemplate'sconfig object currentUser: 'Bob' } This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 4 145 2. Next, we add the extra owner column to our markup and add the first condition for displaying the owner. Initially, we will tell the template to display the word Me when the owner property matches the template's currentUser property: ... '', 'Me', '', ... 3. Finally, we add the else statement to display the plain owner name, if it didn't match the currentUser's value: ... '', '', 'Me', '', '{owner}', '', '', ... How it works... We can perform logic using the tags, which will output the content of the tags if the condition inside the if attribute evaluates to true. We can also build up an if/else construct by adding further tpl tags with the else keyword within it. We do this in Step 3 to output the owner's name, if it isn't the current user's name. The else's tpl tag comes within the initial if's closing tpl tag and simply has the attribute else inside it. We can add further else-if blocks by adding additional tpl tags in exactly the same way as earlier but include an elseif attribute and add another condition as its value. For example, Me* would display Me* if the owner was set to Everyone. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 UI Building Blocks—Trees, Panels, and Data Views 146 There's more… We're not limited to just the use of an if/else statement. We could have coded this recipe with a switch statement: ... '', '', '', 'Me', '', '{owner}', '', '', ... Formatting dates within an Ext.XTemplate Dates are one of the most common data types that need formatting before being displayed to the user. This is no different when creating Ext.XTemplates for use in plain components or more complex components such as Data Views. This recipe will describe the best way to perform this formatting within an Ext.XTemplate. How to do it... 1. First, we will create a simple Ext.XTemplate and render it to the document's body, applying a simple data object, containing a date: var data = { date: '5/8/1986 12:30:00' }; var tpl = new Ext.XTemplate('{date}'); tpl.overwrite(Ext.getBody(), data); // outputs '5/8/1986 12:30:00' 2. We then use the date function, passing it a formatting string to format the date property: var tpl = new Ext.XTemplate('{date:date("Y-m-d")}'); tpl.overwrite(Ext.getBody(), data); // outputs '1986-08-05' This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 4 147 3. We can also use the globally defined patterns that we used in Chapter 2, Manipulating the Dom, Handling Events, and Making AJAX Requests, by concatenating them into the XTemplate's HTML string. var tpl = new Ext.XTemplate('{date:date("' + Ext.Date.patterns. LongDate + '")} '); tpl.overwrite(Ext.getBody(), data); // outputs 'Tuesday, August 05, 1986' How it works... We discussed adding and using member functions within Ext.XTemplates in a previous recipe and you will notice this technique uses identical syntax. The framework provides us with a built-in member function called date, which, just like in the previous recipe, passes the date value into the function as parameter one and returns it in the specified format from parameter two. We simply have to pass it a valid formatting string and the function will output the formatted string. See also ff The recipe, Parsing, formatting, and manipulating dates in Chapter 2, Manipulating the Dom, Handling Events, and Making AJAX Requests, for a more detailed introduction to working with dates. Creating a DataView bound to a data store DataViews are a very useful component that allow us to render markup that is bound to an Ext.data.Store instance. This means that the View renders a defined template for each of the Model instances within the store and will automatically react to changes made to the store, and its data, by refreshing the rendered markup to reflect these changes. By using this approach we can concentrate on manipulating data without needing to worry about how that data is presented because the framework takes care of it for us. In this recipe, we will create a simple store containing data about software bugs and bind it to a data view that will display each of the bugs. We will then demonstrate how changes to the underlying data are automatically reflected in the data view's rendered markup. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 UI Building Blocks—Trees, Panels, and Data Views 148 How to do it... 1. We start by creating a simple Ext.data.Model to represent our bug data. This Model contains five fields—id, title, description, severity, and status: Ext.define('Bug', { extend: 'Ext.data.Model', fields: ['title', 'description', 'status', 'severity'] }); 2. Now that we have a Model, we can create an Ext.data.Store that will encompass a collection of bug Models and give it an array of bugs to be populated with: var bugData = [{ id: 1, title: 'Bug 1', description: 'Bug 1 Description', status: 'In Progress', severity: 1 } ... ]; // only one item shown for brevity var bugStore = new Ext.data.Store({ model: 'Bug', data: bugData }); 3. Our next step is to define our Data View, which will be an instance of the Ext.view. View class. This attaches our previously defined store to it and defines the markup that will be generated for each Model: var dataview = Ext.create('Ext.view.View', { store: bugStore, tpl: '' + '
      ' + '{title}' + '{severity}' + '{description}' + '{status}' + '
      ' + '
      ', itemSelector: 'div.bug-wrapper', emptyText: 'Woo hoo! No Bugs Found!', deferEmptyText: false }); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 4 149 4. Now that we have our Data View defined, we can add it to a wrapping panel that will render it to our document's body: var panel = Ext.create('Ext.panel.Panel', { renderTo: Ext.getBody(), title: 'Creating a DataView bound to a data Store', height: 500, width: 580, layout: 'fit', style: 'margin: 50;', items: [dataview] }); 5. At the moment, if you run this code, our data will look pretty horrible! We can sort this by adding some simple CSS styles targeting the HTML contained in the tpl tag just as we would in a normal HTML page: 6. Although not beautiful, this is a lot better. Finally, we can demonstrate the brilliance of Data Views and the benefit of using them to display data contained in a store. We will add some buttons that will sort, filter, and update the store and we will see how the View redraws itself immediately. We do this by adding the following code to the wrapping panel: .. tbar: [{ xtype: 'combo', name: 'status', This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 4 151 width: 200, labelWidth: 100, fieldLabel: 'Severity Filter', store: ['1', '2', '3', '4', '5'], queryMode: 'local', listeners: { select: function(combo, value, options){ dataview.getStore().clearFilter(); // remove current filters dataview.getStore().filter('severity', combo. getValue()); } } }, '-', { text: 'Sort by Severity', handler: function(){ dataview.getStore().sort('severity', 'DESC'); } }, { text: 'Open all Bugs', handler: function(){ dataview.getStore().each(function(model){ model.set('status', 'Open'); model.commit(); }, this); } }, '->', { text: 'Clear Filter', handler: function(){ dataview.getStore().clearFilter(); } }] ... The following screenshot shows our final DataView with its CSS styles applied and toolbar buttons along the top: This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 UI Building Blocks—Trees, Panels, and Data Views 152 How it works... The Ext.view.View works by rendering a node based on the defined template (within the tpl config option) for each of the store's Models. The View then binds to the store's change events (add, remove, data changed, and so on) and, when fired, refreshes the markup displayed. We can see this happening with the addition of the store manipulations in Step 5. We have only performed a simple sort, filter, or update on the store and the view is automatically updated to reflect this without any prompting from us. See also ff The data package is explored in further detail in Chapter 7, Working with the Ext JS Data Package. The recipes Modeling a data object and loading data into a store from a server, are particularly relevant to this example. Displaying a detailed window after clicking a DataView node In almost every web application, we will want to allow the user to select some data and edit it. Data Views expose a variety of events on each of the rendered nodes and by using these we can give the user the opportunity to interact with the View and perform any number of actions. This recipe, will build on our previous bugs example and will add new functionality; presenting the user with a simple form, allowing them to change, and save the data stored about a specific bug. We will display this form after a single-click on a node and populate the form with that particular node's data. Getting ready We will be building on top of the previous DataView recipe so you may want to look back and quickly refresh your memory. How to do it... 1. We start by creating an instance of Ext.form.Panel containing four form fields, one for each of the bug's data members. The form contains a text field for the bug's title, a text area for its description, a number field to define the bug's severity, and a combo box to allow the status to be changed: var editForm = Ext.create('Ext.form.Panel', { border: false, This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 4 153 items: [{ xtype: 'textfield', name: 'title', width: 300, fieldLabel: 'Title' }, { xtype: 'textarea', name: 'description', width: 300, height: 100, fieldLabel: 'Description' }, { xtype: 'numberfield', name: 'severity', width: 300, fieldLabel: 'Severity', value: 1, minValue: 1, maxValue: 5 }, { xtype: 'combo', name: 'status', width: 300, fieldLabel: 'Status', store: ['Open', 'In Progress', 'Complete'], queryMode: 'local' }] }); 2. We then create an Ext.window.Window instance and add the editForm component to its items collection. This window will be shown when a node is clicked and hidden again after saving. Notice that we have left the save button's handler blank. We will revisit this at the end and add the necessary code: var win = new Ext.window.Window({ height: 250, width: 500, title: 'Edit Bug', modal: true, items: [editForm], closeAction: 'hide', buttons: [{ text: 'Save', handler: function(){ // save logic here } }] }); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 UI Building Blocks—Trees, Panels, and Data Views 154 3. Now that we have our form ready, we can attach an event handler to the itemclick event of the DataView (stored in the dataview variable). When the item (or node) is clicked we populate the editForm with the clicked node's record using the loadRecord method and then show the window (shown in the following screenshot). This code can be added anywhere in our onReady function after the DataView has been instantiated: dataview.on({ itemclick: function(view, record, item, index, e, opts){ // populate the form with the clicked record editForm.loadRecord(record); win.show(); } }); 4. Finally, we implement the Save button's handler code. This code basically updates the bug record that we initially clicked on (that is, the one that is selected) and then closes the window: handler: function(){ // save data var selectedRecord = dataview.selModel.getSelection()[0]; selectedRecord.set(editForm.getValues()); // refilter dataview.getStore().filter(); win.close(); } This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 4 155 How it works... The DataView's itemclick event is fired whenever a single-click is made on any of the rendered nodes. The handlers for this event are passed various useful parameters, such as the record associated with the clicked node, the HTML element of the node, and the index of it. Remember we discussed event delegation in Chapter 2? The Ext.view.View class is a prime example of that concept in action. It uses the defined itemSelector config option to pick out the events on its nodes and fire its own custom events back to us. By using these parameters we are able to use the loadRecord method of Ext.form.Panel, which takes a Model instance (or record) and matches its fields with the corresponding form fields in the panel. This relationship is based on each form field's name property. When saving the edited values (Step 4) we retrieve the record that is currently selected in the DataView by accessing its selection model (through the selModel property) and subsequently the getSelection method. Once this has been retrieved we can use its set method, which accepts a JavaScript object of name/value pairs, among other formats, to update it with the edit form's values. There's more... DataViews expose a huge number of other useful events that we can bind to and have our application react appropriately. We will describe a couple of the most popular here. For a full list of events and their parameters check out the online documentation. itemcontextmenu This event fires when an item is right-clicked by the user. This event could be used to create and display a menu with various actions. itemdblclick When an item is double-clicked this event is fired. selectionchange This is a very useful event that fires whenever the DataView's selected node(s) changes. We could, for example, listen for this event in order to maintain a status bar containing a count of the selected items, similar to Windows Explorer. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 UI Building Blocks—Trees, Panels, and Data Views 156 See also ff If you want to know more about handling events, the recipe Handling events on elements and components in Chapter 2, may be useful. ff Forms and form fields are covered in greater depth over the next two chapters. The recipe Constructing a complex form layout, in Chapter 5, Loading, Submitting, and Validating Forms, is a good place to start. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 5 Loading, Submitting, and Validating Forms In this chapter, we will cover the following topics: ff Constructing a complex form layout ff Populating your form with data ff Submitting your form's data ff Validating form fields with VTypes ff Creating custom VTypes ff Uploading files to the server ff Handling exceptions and callbacks Introduction This chapter introduces forms in Ext JS 4. We begin by creating a support ticket form in the first recipe. To get the most out of this chapter you should be aware that this form is used by a number of recipes throughout the chapter. Instead of focussing on how to configure specific fields, we demonstrate more generic tasks for working with forms. Specifically, these are populating forms, submitting forms, performing client-side validation, and handling callbacks/exceptions. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Loading, Submitting, and Validating Forms 158 Constructing a complex form layout In the previous releases of Ext JS, complicated form layouts were quite difficult to achieve. This was due to the nature of the FormLayout, which was required to display labels and error messages correctly, and how it had to be combined with other nested layouts. Ext JS 4 takes a different approach and utilizes the Ext.form.Labelable mixin, which allows form fields to be decorated with labels and error messages without requiring a specific layout to be applied to the container. This means we can combine all of the layout types the framework has to offer (which are discussed in detail in Chapter 3, Laying Out your Components) without having to overnest components in order to satisfy the form field's layout requirements. We will describe how to create a complex form using multiple nested layouts and demonstrate how easy it is to get a form to look exactly as we want. Our example will take the structure of a Support Ticket Request form and, once we are finished, it will look like the following screenshot: This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 5 159 How to do it... 1. We start this recipe by creating a simple form panel that will contain all of the layout containers and their fields: var formPanel = Ext.create('Ext.form.Panel', { title: 'Support Ticket Request', width: 650, height: 500, renderTo: Ext.getBody(), style: 'margin: 50px', items: [] }); 2. Now, we will create our first set of fields—the FirstName and LastName fields. These will be wrapped in an Ext.container.Container component, which is given an hbox layout so our fields appear next to each other on one line: var formPanel = Ext.create('Ext.form.Panel', { title: 'Support Ticket Request', width: 650, height: 500, renderTo: Ext.getBody(), style: 'margin: 50px', items: [{ xtype: 'container', layout: 'hbox', items: [{ xtype: 'textfield', fieldLabel: 'First Name', name: 'FirstName', labelAlign: 'top', cls: 'field-margin', flex: 1 }, { xtype: 'textfield', fieldLabel: 'Last Name', name: 'LastName', labelAlign: 'top', cls: 'field-margin', flex: 1 }] }] }); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Loading, Submitting, and Validating Forms 160 3. We have added a CSS class (field-margin) to each field, to provide some spacing between them. We can now add this style inside 4. Next, we create a container with a column layout to position our e-mail address and telephone number fields. We nest our telephone number fields in an Ext.form.FieldContainer class, which we will discuss later in the recipe: items: [ ... { xtype: 'container', layout: 'column', items: [{ xtype: 'textfield', fieldLabel: 'Email Address', name: 'EmailAddress', labelAlign: 'top', cls: 'field-margin', columnWidth: 0.6 }, { xtype: 'fieldcontainer', layout: 'hbox', fieldLabel: 'Tel. Number', labelAlign: 'top', cls: 'field-margin', columnWidth: 0.4, items: [{ xtype: 'textfield', name: 'TelNumberCode', style: 'margin-right: 5px;', flex: 2 }, { xtype: 'textfield', name: 'TelNumber', flex: 4 }] }] } ... ] This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 5 161 5. The text area and checkbox group are created and laid out in a similar way to the previous sets, by using an hbox layout: items: [ ... { xtype: 'container', layout: 'hbox', items: [{ xtype: 'textarea', fieldLabel: 'Request Details', name: 'RequestDetails', labelAlign: 'top', cls: 'field-margin', height: 250, flex: 2 }, { xtype: 'checkboxgroup', name: 'RequestType', fieldLabel: 'Request Type', labelAlign: 'top', columns: 1, cls: 'field-margin', vertical: true, items: [{ boxLabel: 'Type 1', name: 'type1', inputValue: '1' }, { boxLabel: 'Type 2', name: 'type2', inputValue: '2' }, { boxLabel: 'Type 3', name: 'type3', inputValue: '3' }, { boxLabel: 'Type 4', name: 'type4', inputValue: '4' }, { boxLabel: 'Type 5', name: 'type5', inputValue: '5' }, { This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Loading, Submitting, and Validating Forms 162 boxLabel: 'Type 6', name: 'type6', inputValue: '6' }], flex: 1 }] } ... ] 6. Finally, we add the last field, which is a file upload field, to allow users to provide attachments: items: [ ... { xtype: 'filefield', cls: 'field-margin', fieldLabel: 'Attachment', width: 300 } ... ] How it works... All Ext JS form fields inherit from the base Ext.Component class and so can be included in all of the framework's layouts. For this reason, we can include form fields as children of containers with layouts (such as hbox and column layouts) and their position and size will be calculated accordingly. Upgrade Tip: Ext JS 4 does not have a form layout meaning a level of nesting can be removed and the form fields' labels will still be displayed correctly by just specifying the fieldLabel config. The Ext.form.FieldContainer class used in step 4 is a special component that allows us to combine multiple fields into a single container, which also implements the Ext.form. Labelable mixin. This allows the container itself to display its own label that applies to all of its child fields while also giving us the opportunity to configure a layout for its child components. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 5 163 See also ff The recipe explaining mixins found in Chapter 1, Classes, Object-Oriented Principles, and Structuring your Application. ff You may also find Chapter 3, Laying Out your Components useful, which explains how each of the layouts used here work, in particular those about the hbox and column layouts. ff For more details about how to use form fields and elements take a look at Chapter 6, Using and Configuring Form Fields. Populating your form with data After creating our beautifully crafted and user-friendly form we will inevitably need to populate it with some data so users can edit it. Ext JS makes this easy, and this recipe will demonstrate four simple ways of achieving it. We will start by explaining how to populate the form on a field-by-field basis, then move on to ways of populating the entire form at once. We will also cover populating it from a simple object, a Model instance, and a remote server call. Getting ready We will be using the form created in this chapter's first recipe as our base for this section, and many of the subsequent recipes in this chapter, so please look back if you are not familiar with it. All the code we will write in this recipe should be placed under the definition of this form panel. You will also require a working web server for the There's More example, which loads data from an external file. How to do it... We'll demonstrate how to populate an entire form's fields in bulk and also how to populate them individually. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Loading, Submitting, and Validating Forms 164 Populating individual fields 1. We will start by grabbing a reference to the first name field using the items property's get method. The items property contains an instance of Ext.util. MixedCollection, which holds a reference to each of the container's child components. We use its get method to retrieve the component at the specified index: var firstNameField = formPanel.items.get(0).items.get(0); 2. Next, we use the setValue method of the field to populate it: firstNameField.setValue('Joe'); Populating the entire form 1. To populate the entire form, we must create a data object containing a value for each field. The property names of this object will be mapped to the corresponding form field by the field's name property. For example, the FirstName property of our requestData object will be mapped to a form field with a name property value of FirstName: var requestData = { FirstName: 'Joe', LastName: 'Bloggs', EmailAddress: 'info@swarmonline.com', TelNumberCode: '0777', TelNumber: '7777777', RequestDetails: 'This is some Request Detail body text', RequestType: { type1: true, type2: false, type3: false, type4: true, type5: true, type6: false } }; 2. We then call the setValues method of the form panel's Ext.form.Basic instance, accessed through the getForm method, passing it our requestData variable: formPanel.getForm().setValues(requestData); How it works... Each field contains a method called setValue, which updates the field's value with the value that is passed in. We can see this in action in the first part of the How to do it section. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 5 165 A form panel contains an internal instance of the Ext.form.Basic class (accessible through the getForm method), which provides all of the validation, submission, loading, and general field management that is required by a form. This class contains a setValues method, which can be used to populate all of the fields that are managed by the basic form class. This method works by simply iterating through all of the fields it contains and calling their respective setValue methods. This method accepts either a simple data object, as in our example, whose properties are mapped to fields based on the field's name property. Alternatively, an array of objects can be supplied, containing id and value properties, with the id mapping to the field's name property. The following code snippet demonstrates this usage: formPanel.getForm().setValues([{id: 'FirstName', value: 'Joe'}]); There's more... Further to the two previously discussed methods there are two others that we will demonstrate here. Populating a form from a Model instance Being able to populate a form directly from a Model instance is extremely useful and is very simple to achieve. This allows us to easily translate our data structures into a form without having to manually map it to each field. We initially define a Model and create an instance of it (using the data object we used earlier in the recipe): Ext.define('Request', { extend: 'Ext.data.Model', fields: [ 'FirstName', 'LastName', 'EmailAddress', 'TelNumberCode', 'TelNumber', 'RequestDetails', 'RequestType' ] }); var requestModel = Ext.create('Request', requestData); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Loading, Submitting, and Validating Forms 166 Following this we call the loadRecord method of the Ext.form.Basic class and supply the Model instance as its only parameter. This will populate the form, mapping each Model field to its corresponding form field based on the name: formPanel.getForm().loadRecord(requestModel); Populating a form directly from the server It is also possible to load a form's data directly from the server through an AJAX call. Firstly, we define a JSON file, containing our request data, which will be loaded by the form: { "success": true, "data": { "FirstName": "Joe", "LastName": "Bloggs", "EmailAddress": "info@swarmonline.com", "TelNumberCode": "0777", "TelNumber": "7777777", "RequestDetails": "This is some Request Detail body text", "RequestType": { "type1": true, "type2": false, "type3": false, "type4": true, "type5": true, "type6": false } } } Notice the format of the data: we must provide a success property to indicate that the load was successful and put our form data inside a data property. Next we use the basic form's load method and provide it with a configuration object containing a url property pointing to our JSON file: formPanel.getForm().load({ url: 'requestDetails.json' }); This method automatically performs an AJAX request to the specified URL and populates the form's fields with the data that was retrieved. This is all that is required to successfully load the JSON data into the form. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 5 167 The basic form's load method accepts similar configuration options to a regular AJAX request, which are discussed in the Loading Data through AJAX recipe, in Chapter 2. See also ff The previous recipe, which shows how to create the form we have used in this recipe's examples. ff The recipes explaining the Ext.data package, which include details of Models. ff To learn about submitting forms see the next recipe in this chapter. Submitting your form's data Having taken care of populating the form it's now time to look at sending newly added or edited data back to the server. As with form population you'll learn just how easy this is with the Ext JS framework. There are two parts to this example. Firstly, we will submit data using the options of the basic form that wraps the form panel. The second example will demonstrate binding the form to a Model and saving our data. Getting ready We will be using the form created in the first recipe as our base for this section, so refer to the Constructing a complex form layout recipe, if you are not familiar with it. How to do it... 1. Add a function to submit the form: var submitForm = function(){ formPanel.getForm().submit({ url: 'submit.php' }); }; This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Loading, Submitting, and Validating Forms 168 2. Add a button to the form that calls the submitForm function: var formPanel = Ext.create('Ext.form.Panel', { ... buttons: [{ text: 'Submit Form', handler: submitForm }], items: [ ... ] }); How it works... As we learned in the previous recipe, a form panel contains an internal instance of the Ext.form.Basic class (accessible through the getForm method). The submit method in Ext.form.Basic is a shortcut to the Ext.form.action.Submit action. This class handles the form submission for us. All we are required to do is provide it with a URL and it will handle the rest. It's also possible to define the URL in the configuration for the Ext.form.Panel. Before submitting, it must first gather the data from the form. The Ext.form.Basic class contains a getValues method, which is used to gather the data values for each form field. It does this by iterating through all fields in the form making a call to their respective getValue methods. There's more... The previous recipe demonstrated how to populate the form from a Model instance. Here we will take it a step further and use the same Model instance to submit the form as well. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 5 169 Submitting a form from a Model instance 1. Extend the Model with a proxy and load the data into the form: Ext.define('Request', { extend: 'Ext.data.Model', fields: ['FirstName', 'LastName', 'EmailAddress', 'TelNumberCode', 'TelNumber', 'RequestDetails', 'RequestType'], proxy: { type: 'ajax', api: { create: 'addTicketRequest.php', update: 'updateTicketRequest.php' }, reader: { type: 'json' } } }); var requestModel = Ext.create('Request', { FirstName: 'Joe', LastName: 'Bloggs', EmailAddress: 'info@swarmonline.com' }); formPanel.getForm().loadRecord(requestModel); 2. Change the submitForm function to get the Model instance, update the record with the form data, and save the record to the server: var submitForm = function(){ var record = formPanel.getForm().getRecord(); formPanel.getForm().updateRecord(record); record.save(); }; See Also ff The previous recipe which shows how to create the form we have used in this recipe's examples. ff The basic form's submit method accepts similar configuration options as a regular AJAX request which are discussed in the Loading data through AJAX recipe, in Chapter 2. ff To learn about loading forms see the previous recipe in this chapter. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Loading, Submitting, and Validating Forms 170 Validating form fields with VTypes In addition to form fields' built-in validation (such as allowBlank and minLength), we can apply more advanced and more extensible validation by using VTypes. A VType (contained in the Ext.form.field.VTypes singleton) can be applied to a field and its validation logic will be executed as part of the field's periodic validation routine. A VType encapsulates a validation function, an error message (which will be displayed if the validation fails), and a regular expression mask to prevent any undesired characters from being entered into the field. This recipe will explain how to apply a VType to the e-mail address field in our example form, so that only properly formatted e-mail addresses are deemed valid and an error will be displayed if it doesn't conform to this pattern. How to do it... 1. We will start by defining our form and its fields. We will be using our example form that was created in the first recipe of this chapter as our base. 2. Now that we have a form we can add the vtype configuration option to our e-mail address field: { xtype: 'textfield', fieldLabel: 'Email Address', name: 'EmailAddress', labelAlign: 'top', cls: 'field-margin', columnWidth: 0.6, vtype: 'email' } 3. That is all we have to do to add e-mail address validation to a field. We can see the results in the following screenshot, with an incorrectly formatted e-mail address on the left and a valid one on the right: This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 5 171 How it works... When a field is validated it runs through various checks. When a VType is defined the associated validation routine is executed and will flag the field invalid or not . As previously mentioned, each VType has an error message coupled with it, which is displayed if it is found to be invalid, and a mask expression which prevents unwanted characters being entered. Unfortunately, only one VType can be applied to a field and so, if multiple checks are required, a custom hybrid may need to be created. See the next recipe for details on how to do this. There's more... Along with the e-mail VType, the framework provides three other VTypes that can be applied straight out of the box. These are: ff alpha: this restricts the field to only alphabetic characters ff alphnum: this VType allows only alphanumeric characters ff url: this ensures that the value is a valid URL See also ff See the next recipe that demonstrates how to create your own custom VTypes. ff The recipe about displaying validation alerts to the user later in this chapter. Creating custom VTypes We have seen in the previous recipe how to use VTypes to apply more advanced validation to our form's fields. The built-in VTypes provided by the framework are excellent but we will often want to create custom implementations to impose more complex and domain specific validation to a field. We will walkthrough creating a custom VType to be applied to our telephone number field to ensure it is in the format that a telephone number should be. Although our telephone number field is split into two (the first field for the area code and the second for the rest of the number), for this example we will combine them so our VType is more comprehensive. For this example, we will be validating a very simple, strict telephone number format of "0777-777-7777". This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Loading, Submitting, and Validating Forms 172 How to do it... 1. We start by defining our VType's structure. This consists of a simple object literal with three properties. A function called telNumber and two strings called telNumberText (which will contain the error message text) and telNumberMask (which holds a regex to restrict the characters allowed to be entered into the field) respectively. var telNumberVType = { telNumber: function(val, field){ // function executed when field is validated // return true when field's value (val) is valid return true; }, telNumberText: 'Your Telephone Number must only include numbers and hyphens.', telNumberMask: /[\d\-]/ }; 2. Next we define the regular expression that we will use to validate the field's value. We add this as a variable to the telNumber function: telNumber: function(val, field){ var telNumberRegex = /^\d{4}\-\d{3}\-\d{4}$/; return true; } 3. Once this has been done we can add the logic to this telNumber function that will decide whether the field's current value is valid. This is a simple call to the regular expression string's test method, which returns true if the value matches or false if it doesn't: telNumber: function(val, field){ var telNumberRegex = /^\d{4}\-\d{3}\-\d{4}$/; return telNumberRegex.test(val); } 4. The final step to defining our new VType is to apply it to the Ext.form.field. VTypes singleton, which is where all of the VTypes are located and where our field's validation routine will go to get its definition: Ext.apply(Ext.form.field.VTypes, telNumberVType); This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 5 173 5. Now that our VType has been defined and registered with the framework, we can apply it to the field by using the vtype configuration option. The result can be seen in the following screenshot: { xtype: 'textfield', name: 'TelNumber', flex: 4, vtype: 'telNumber' } How it works... A VType consists of three parts: ff The validity checking function ff The validation error text ff A keystroke filtering mask (optional) VTypes rely heavily on naming conventions so they can be executed dynamically within a field's validation routine. This means that each of these three parts must follow the standard convention. The validation function's name will become the name used to reference the VType and form the prefix for the other two properties. In our example, this name was telNumber, which can be seen referencing the VType in Step 5. The error text property is then named with the VType's name prefixing the word Text (that is, telNumberText). Similarly, the filtering mask is the VType's name followed by the word Mask (that is, telNumberMask). The final step to create our VType is to merge it into the Ext.form.field.VTypes singleton allowing it to be accessed dynamically during validation. The Ext.apply function does this by merging the VType's three properties into the Ext.form.field.VTypes class instance. When the field is validated, and a vtype is defined, the VType's validation function is executed with the current value of the field and a reference to the field itself being passed in. If the function returns true then all is well and the routine moves on. However, if it evaluates to false the VType's Text property is retrieved and pushed onto the errors array. This message is then displayed to the user as our screenshot shown earlier. This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Loading, Submitting, and Validating Forms 174 This process can be seen in the code snippet as follows, taken directly from the framework: if (vtype) { if(!vtypes[vtype](value, me)){ errors.push(me.vtypeText || vtypes[vtype +'Text']); } } There's more... It is often necessary to validate fields based on the values of other fields as well as their own. We will demonstrate this by creating a simple VType for validating that a confirm password field's value matches the value entered in an initial password field. We start by creating our VType structure as we did before: Ext.apply(Ext.form.field.VTypes, { password: function(val, field){ return false; }, passwordText: 'Your Passwords do not match.' }); We then complete the validation logic. We use the field's up method to get a reference to its parent form. Using that reference, we get the values for all of the form's fields by using the getValues method: password: function(val, field){ var parentForm = field.up('form'); // get parent form // get the form's values var formValues = parentForm.getValues();
 return false; } The next step is to get the first password field's value. We do this by using an extra property (firstPasswordFieldName) that we will specify when we add our VType to the confirm password field. This property will contain the name of the initial password field (in this example Password). We can then compare the confirm password's value with the retrieved value and return the outcome: password: function(val, field){ var parentForm = field.up('form'); // get parent form // get the form's values This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 5 175 var formValues = parentForm.getValues(); // get the value from the configured 'First Password' field var firstPasswordValue = formValues[field.firstPasswordFieldName]; // return true if they match return val === firstPasswordValue; } The VType is added to the confirm password field in exactly the same way as before but we must include the extra firstPasswordFieldName option to link the fields together: { xtype: 'textfield', fieldLabel: 'Confirm Password', name: 'ConfirmPassword', labelAlign: 'top', cls: 'field-margin', flex: 1, vtype: 'password', firstPasswordFieldName: 'Password' } See also ff For an introduction to VTypes see the previous recipe. ff The recipe titled Displaying validation alerts to the user, in this chapter. Uploading files to the server Uploading files is very straightforward with Ext JS 4. This recipe will demonstrate how to create a basic file upload form and send the data to your server: This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Loading, Submitting, and Validating Forms 176 Getting Ready This recipe requires the use of a web server for accepting the uploaded file. A PHP file is provided to handle the file upload; however, you can integrate this Ext JS code with any server-side technology you wish. How to do it... 1. Create a simple form panel. Ext.create('Ext.form.Panel', { title: 'Document Upload', width: 400, bodyPadding: 10, renderTo: Ext.getBody(), style: 'margin: 50px', items: [], buttons: [] }); 2. In the panel's items collection add a file field: Ext.create('Ext.form.Panel', { ... items: [{ xtype: 'filefield', name: 'document', fieldLabel: 'Document', msgTarget: 'side', allowBlank: false, anchor: '100%' }], buttons: [] }); 3. Add a button to the panel's buttons collection to handle the form submission: Ext.create('Ext.form.Panel', { ... buttons: [{ text: 'Upload Document', handler: function(){ var form = this.up('form').getForm(); if (form.isValid()) { form.submit({ url: 'upload.php', waitMsg: 'Uploading...' This material is copyright and is licensed for the sole use by Gauthier Giacomoni on 11th September 2012 47 gordon street #4, Allston, 02134 Chapter 5 177 }); } } }] }); How it works... Your server-side code should handle these form submissions in the same way they would handle a regular HTML file upload form. You should not have to do anything special to make your server-side code compatible with Ext JS. The example works by defining an Ext.form.field.File (xtype: 'filefield'), which takes care of the styling and the button for selecting local files. The form submission handler works the same way as any other form submission; however, behind the scenes the framework tweaks how the form is submitted to the server. A form with a file upload field is not submitted using an XMLHttpRequest object—instead the framework creates and submits a temporary hidden
      element whose target is referenced to a temporary hidden