Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExtJs 4... How to extend Extjs 4 components?

Tags:

can somebody help me with how to extend extjs components using extjs version 4. I am looking for a proper syntax for the same. please help..!!

like image 430
Riya Avatar asked Jul 07 '11 10:07

Riya


People also ask

What is extend in ExtJS?

Explanation. ExtJS provides two main ways to change the behavior of existing classes: extending them, and overriding them. Each has benefits and pitfalls that should be considered before using them. Extensions. Extending a class creates a new class that inherits its behavior and configuration from its parent.

What is initComponent in ExtJS?

initComponent This method is invoked by the constructor. It is used to initialize data, set up configurations, and attach event handlers. beforeShow This method is invoked before the Component is shown.

What creates ext?

Ext. create - to create an instance of a pre-defined class. - the class was defined using Ext. define - used to define the data and behavior of a component. which will be used later.

What is the use of Ext?

Ext JS is a popular JavaScript framework which provides rich UI for building web applications with cross-browser functionality. Ext JS is basically used for creating desktop applications. It supports all the modern browsers such as IE6+, FF, Chrome, Safari 6+, Opera 12+, etc.


2 Answers

Following is an example code of extending textfield in ExtJS 4.

Other then using the existing configs and methods, this extended component also has a new config property created and a new method created & associated with an event.

The purpose of component is simple that it displays the label in red color if the value is mandatory, modifies the background color of the field if its readOnly and also changes the background color of the field when focussed.

The code is properly commented. Hope it helps.

Ext.define('Ext.pnc.Textfield', {

extend: 'Ext.form.field.Text',//Extending the TextField

alias: 'widget.pnctextfield',//Defining the xtype

config:{
    focusCls:'focusClassFieldPnC',//Providing value for existing config property.
    testConfig:'testConfigValue'//Creating a new config. Accessor functions will be created for this one by ExtJS engine
},

constructor:function(cnfg){
    this.callParent(arguments);//Calling the parent class constructor
    this.initConfig(cnfg);//Initializing the component
    this.on('beforerender',this.beforeRender);//Associating a new defined method with an event
},

//Defining a method below and associating this with an event in the constructor above
beforeRender:function(){

    if(!this.allowBlank){
        this.labelStyle =   'color:#FF0000';        
    }

    if(this.readOnly){
        this.fieldCls   =   'readOnlyClass';
    }
},

//Over-riding a function which already exists in parent class. Note that this has not been associated with any event in constructor as it already defined in parent class
afterRender:function(){
    console.log('after render function');
    this.callParent();//Calling the parent class method. This can be omitted if not     required and is not a must
}
});

.readOnlyClass{
background:#FF0000 !important
}


.focusClassFieldPnC{
background:#00ff00 !important
}
like image 129
netemp Avatar answered Sep 29 '22 10:09

netemp


Ext.define('myApp.Grid', {
        extend: 'Ext.Grid',
        alias: 'widget.mygrid'
        ....
        ....
        }

now you can use xtype:'mygrid'

like image 35
Kunal Avatar answered Sep 29 '22 10:09

Kunal