0

我仍在学习 EXTJ,而我试图做的一件事就是扩展一个组件。下面是我的例子:

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);

} 

我想调用一个控制器方法来验证用户是否有权查看此字段并相应地执行下一步操作。我希望当我导航到
我在视图中使用此自定义字段的视图时进行此验证:

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

但是 initComponent 永远不会被触发。甚至在我的商店加载之前,初始化很快就会被触发。如何正确扩展此控件?

4

1 回答 1

0

在 ExtJS 6 现代中, textfield没有initComponent事件。文本字段的经典事件。initComponent

对于调用事件,controller您需要为您创建controller和定义view

在这个FIDDLE中,我使用form、和. 我希望这将帮助/指导您实现您的要求。ViewControllertextfieldViewModel

有关详细信息,请参阅 ExtJS文档

代码片段

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: 'narendrajadhav105@gmail.com'
                },
                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}'
                    }
                }]
            }]
        });
    }
});
于 2018-03-08T06:46:18.797 回答