Skip to main content

EXTJS Best Practices



ExtJS is a powerful Javascript framework for developing rich Internet applications—but with great power comes great responsibility. If you are inexperienced or careless with your implementation, your development effort can quickly spin out of control. Your project structure might grow to become unruly, steepening the learning curve for developers new to the framework. Worse yet, performance issues may arise that can be tricky to deal with once they have a foothold within your application.
Equipped with some tips from Senchacon ‘13 and my past experience with ExtJS, I’d like to share some knowledge that will help you be proactive against these issues and avoid potential roadblocks.

Folder and Code Structure

First, two pieces of advice that are not specific to ExtJS, but still helpful:
Maintain an organized project structure. Placing every view under “views” in a directory is likely to cause headaches somewhere down the line, especially if a developer unfamiliar with the project needs to find the source of a particular view.
Group views in sub-folders based on relevance and function. As the project grows and the number of views increases, more tiers should be added to your folder structure. Adding another folder in the hierarchy will have very little impact in the execution of the application, but has the potential to save some frustration.
Your source code should conform to a coding standard. A set standard will help fight against spaghetti code, promote reusability, and facilitate learning. When in doubt, the best code structure you can conform to is the ExtJS source code itself.

Ext.override

Ext.override is a function that allows you to override classes and add or change any functionality you desire. It’s important to understand that if you override a base class, the functionality will affect every instance of that class and any classes that inherit from it. There’s a good chance you will introduce a few defects in the process. Also, if you decide to upgrade to a newer version of ExtJS, there’s a chance your override will no longer work as intended.
There will be times when it is necessary to override a class. In these cases, be sure to use best practices when applying the overrides. Do not just place all of your overrides in one file and let it grow over time. Break them apart into their own succinct files and make plenty of comments to describe the reasoning behind and outcome of your changes. It will save time if another developer has to revisit that override after a long period of time.

Excessive Layouts

Layouts can be the bane of an ExtJS application’s existence. They can be triggered by adding or removing new components, changing a component’s content or size, updating a record displayed in a grid with a custom renderer, and much more.
Any time you have a code block that is making these types of changes be sure to surround it with:
suspendLayouts();
     #code causing layouts to occur
resumeLayouts(true);
Or if you are CRUDing records in a store that is causing a grid to perform multiple layouts, consider using:
gridStore.suspendEvents(); 
      #CRUD operations
gridStore.resumeEvents();

Large Grid Datasets

BufferedRenderer is a great plugin to use with grid panels when dealing with large datasets. Essentially this acts as an infinite scrolling solution and rows are only rendered onto the screen when they need to be, rather than all at once.
var grid = Ext.create('Ext.grid.Panel', { 
     //..
     autoLoad: true, 
     plugins: {
        ptype: 'bufferedrenderer',
        trailingBufferZone: 10,
        leadingBufferZone: 20,
        numFromEdge: 6
    },
    //.. 
});
The config options to note are:
- trailingBufferZone – the number of rows rendered above the table
- leadingBufferZone – the number of rows rendered below the table
- numFromEdge – the number of rows from the end of the leading or trailing buffer zones that will cause more rows to be rendered into the table

Lazy Instantiation

Attempt to lazily instantiate your items array wherever possible. A primary example would be the hidden tabs of a tabpanel. If the parent container never gets rendered, the child items aren’t instantiated. The plugin here will allow you to accomplish this.
{
    xtype: 'tabpanel',
    items: [{
        title: 'T1',
        plugins: {
            ptype: 'lazyitems',
            items: [...]
        }
},    {
        title: 'T2',
        plugins: {
            ptype:'lazyitems',
            items: [...]
        }
    }, {...}]
}

Nesting

Overnesting occurs if a component has a single item in its list, which in turn has a single item of its own that defines the rest of the components in the view. The item in the middle should be omitted completely, if the functionality remains the same. Doing so keeps the application structure simple to help reduce the DOM burden and speed up your application.
This bookmarklet helps to automate the search and point you toward problem areas where your application might have some unneeded nesting. A simple way to run this is to copy and paste the code from Gist into your browser’s developer console while the application is running. You will notice areas of the app highlight in red and any problem areas will be logged in the console.

References:
http://www.sencha.com/blog/ext-js-4-1-performance/

Popular posts from this blog

ExtJS - Grid panel features

What can we do with ExtJS GridPanel? I have to develop a lot of applications in my web app and I see that grid component of ExtJS may fit in. However, I am not aware of what all things I can do with the - off the shelf available framework pieces - available plug-ins in the marketplace and - custom development through my own developers This is a typical question that we hear from the business users who wants to design an application by keeping the framework’s capability in perspective. In this article I have tried to put the list of stuff you can do with grid and hopefully that shall enable you to take advantage of the beauty of ExtJS. Pre-requisites This article assumes that you are familiar with basics of ExtJS What are the available options? In this section I will be taking you through some of the commonly seen usage of ExtJS grid panel. While covering all the capabilities of grid may not be possible, I am sure it will be helpful for the business users who want to...

ExtJS 4 with Spring MVC Example

Introduction When developing a brand new application for the company I work for, one of the first thing I implement is "authentication". Not only is this process generally a prerequisite to access many other functions but it is also very simple and covers every layer of an application, from GUI to the database. So instead of another minimalistic "Hello World" tutorial, here is a "Login/Password" example, using 2 famous technologies : Spring MVC and ExtJS 4.   Prerequisites Because this tutorial is quite long, I won't go into the details of each technology I used but I will mainly focus on how ExtJS and Spring can be nicely used together. So here are the things you should know before continuing : Spring framework (good knowledge) Spring MVC (basic knowledge. See  Spring MVC Official Documentation  for details) ExtJS "application architecture" (see  Sencha Docs  for details) Eclipse IDE + Tomcat 6 (or any other web container) You a...

Ext4 Apply Store Filtering

In extjs4.1: There are many way for store filtering . Some of code i give here Filtering by single field: store . filter ( 'eyeColor' , 'Brown' );   Alternatively, if filters are configured with an  id , then existing filters store may be  replaced by new filters having the same  id . Filtering by single field: store . filter ( "email" , /\.com$/ );   Using multiple filters: store . filter ([ { property : "email" , value : /\.com$/ }, { filterFn : function ( item ) { return item . get ( "age" ) > 10 ; }} ]);   Using  Ext.util.Filter  instances instead of config objects (note that we need to specify the root config option in this case): store . filter ([ Ext . create ( ' Ext.util.Filter ' , {   property : "email" , value : /\.com$/ , root : 'data' }),   Ext . create ( ' Ext.util.Filter ' , {   filterFn : function ( item ) {   return item . get ( ...