Search code examples
extjsextjs6-modernextjs6.2

Extjs6.2 Modern toolkit- Extend a textbox


I am still learning EXTJs and one of the thing I was trying to to was extend a component. Below is my example:

Ext.define('MyApp.view.CustomTextField',{
extend: 'Ext.field.Text',
xtype: 'customtextfield',

config:
{
    fieldID: null,
    langID: null
},
initialize: function() {
    alert("init"); //1. called before I navigate to view
     Call a controller method here
    this.callParent(arguments);
},
initComponent: function () {
    alert("initComp"); //2. not called at all
    Call a controller method here
    this.callParent(arguments);

} 

I want to call a controller method to validate if user has permission to see this field and accordingly do next actions. I want this validation to happen when I navigate to the view
I used this custom field in my View as:

xtype: 'fieldset',
        margin: 10,
        bind: '{workOrders}',
        title: 'Dispatch Information',
        items: [
            {   
                id: 'Tag',
                xtype: 'customtextfield',
                name: 'Tag',
                label: 'Tag',
                bind: '{Tag}',
                labelAlign: 'top'
            },

But the initComponent is never fired. The initialize is fired to soon ,even before my stores are loaded. How do I properly extend this control?


Solution

  • In ExtJS 6 modern there is no initComponent event for textfield . initComponent event have in classic for textfield.

    For calling events in controller you need to create controller and define to you view.

    In this FIDDLE, I have created a demo using form, ViewController, textfield and ViewModel. I hope this will help/guide you to achieve your requirements.

    For more details please refer ExtJS Docs.

    CODE SNIPPET

    Ext.application({
        name: 'Fiddle',
    
        launch: function () {
    
            //Define the cutsometext from extending {Ext.field.Text}
            Ext.define('CustomText', {
                extend: 'Ext.field.Text',
                xtype: 'customtext',
                labelAlign: 'top',
                listeners: {
                    initialize: 'onInitializeCutsomText'
                }
            });
    
            Ext.define('FormModel', {
                extend: 'Ext.app.ViewModel',
                alias: 'viewmodel.formmodel',
    
                data: {
                    user: {
                        firstName: 'Narendra',
                        lastName: 'Jadhav',
                        email: '[email protected]'
                    },
                    permissionCng: {
                        firstName: false,
                        lastName: false,
                        email: true,
                        isAdmin: false
                    }
                }
    
            });
            //Define the FormController from extending {Ext.app.ViewController}
            Ext.define('FormController', {
                extend: 'Ext.app.ViewController',
                alias: 'controller.formctn',
    
                onInitializeCutsomText: function (textfield) {
                    var permissionCng = this.getViewModel().get('permissionCng');
                    // Here is just basic example for disabled textfield on initialize event.
                    //In your case you can put your requirement.
                    textfield.setDisabled(permissionCng[textfield.getName()]);
                }
            });
    
            //Creating form
            Ext.create('Ext.form.Panel', {
                fullscreen: true,
                viewModel: {
                    type: 'formmodel'
                },
                controller: 'formctn',
                items: [{
                    xtype: 'fieldset',
                    title: 'Personal Info',
                    defaults: {
                        xtype: 'customtext' //Here I am using {customtext}
                    },
                    items: [{
                        label: 'First Name',
                        name: 'firstName',
                        bind: {
                            value: '{user.firstName}',
                            //You can also use like property
                            //hidden:'{permissionCng.firstName}'
                        }
                    }, {
                        label: 'Last Name',
                        name: 'lastName',
                        bind: {
                            value: '{user.lastName}',
                            //You can also use like property
                            //hidden:'{permissionCng.firstName}'
                        }
                    }, {
                        label: 'Email Id',
                        name: 'email',
                        bind: {
                            value: '{user.email}',
                            //You can also use like property
                            //hidden:'{permissionCng.firstName}'
                        }
                    }, {
                        label: 'Admin Name',
                        name: 'isAdmin',
                        bind: {
                            value: '{user.isAdmin}',
                            //You can also use like property
                            hidden: '{!permissionCng.isAdmin}'
                        }
                    }]
                }]
            });
        }
    });