ExtJS:全局函数在页面加载之前执行。如何延迟?

ExtJS: Global function executes before page load. How to delay?

我在 panel items 中有一个 combobox 并在 combobox listeners 中放置了一个 select 事件。事件的函数调用全局方法,但此全局函数在页面加载期间执行。

我在几个 类 上使用这个组合框 selection 方法。因此,全局函数将很容易。因此需要在组合框的项目 selection 中以正常行为使用。我怎样才能实现这个调整?

全局函数:

Ext.define('MyApp.BaseMethods', {
    requires: [],
    singleton: true,

    // Instead of using method content several times; passing comboname and required JSON value on arguments
    comboQuery: function(comboname, JSONValue) {
        var query = Ext.ComponentQuery.query(comboname)[0].getSelectedRecord();
        var requiredValue = query.get(JSONValue);

        return requiredValue;
    },
});

面板:

Ext.define('MyApp.view.FooPanel', {
     extend: 'Ext.panel.Panel',
     items: [{
         xtype: 'foocombo',
         listeners: {
             // Trying to pass required information through parameters
             select: MyApp.BaseMethods.comboQuery('[name=myfoocombo]', 'foojsonvalue'),
             scope: me
         }
     }]
 });

运行此方法时;全局函数在 button 页面加载期间运行,单击显示 FooPanel 及其项,并由于无法 select 组合项而出错。;

Uncaught TypeError: Cannot read property 'getSelectedRecord' of undefined

How to delay?

不用耽误,只需要换个方式

由于您向 select 提供了一个函数,它将在页面加载或组件创建时调用,因此您需要在 select 事件函数内部调用。

I'm using this combobox selection method on several classes. Therefore a global function will be to easy. So need to use with normal behaviour during combobox's items selection. How can I achieve to this adjustment?

所以正如你提到的那样我在几个类上使用这个组合框选择方法为此你可以创建一个公共组件并且你可以轻松获得价值在那个公共组件 select 事件上。

例子

{
    xtype: 'combobox',
    listeners: {
        function: select(combo, record, eOpts) {
            //you can get easily value here
            //{record} With multiSelect false, the value will be a single record. With multiSelect true, the value will be an array of records.
            record.get('you json name');
        }
    }
}

在此FIDDLE中,我创建了一个演示。希望这会 help/guide 你达到你的要求。

代码片段

Ext.application({
    name: 'Fiddle',

    launch: function () {

        //Common Component
        //I'm using this combobox selection method on several classes.
        Ext.define('foocombo', {
            extend: 'Ext.form.field.ComboBox',
            xtype: 'foocombo',
            store: {
                fields: ['abbr', 'name'],
                data: [{
                    "abbr": "AL",
                    "name": "Alabama"
                }, {
                    "abbr": "AK",
                    "name": "Alaska"
                }, {
                    "abbr": "AZ",
                    "name": "Arizona"
                }]
            },
            fieldLabel: 'Choose State',
            queryMode: 'local',
            displayField: 'name',
            valueField: 'abbr',
            JSONValue: 'name',
            listeners: {
                // Trying to pass required information through parameters
                select: function (combo, record) {
                    //As per simple way
                    console.log(record.get('name'));
                    //As per you implement
                    console.log(MyApp.BaseMethods.comboQuery(`[name=${combo.getName()}]`, combo.JSONValue))
                }
            }
        })

        Ext.define('MyApp.BaseMethods', {
            singleton: true,

            // Instead of using method content several times; passing comboname and required JSON value on arguments
            comboQuery: function (comboname, JSONValue) {
                var query = Ext.ComponentQuery.query(comboname)[0];
                if (query) {
                    var rec = query.getSelectedRecord();
                    return rec.get(JSONValue) || null;
                }
                return null
            }
        });

        Ext.define('MyApp.view.FooPanel', {
            extend: 'Ext.panel.Panel',
            xtype: 'foopanel',
            items: [{
                xtype: 'foocombo',
                name: 'myfoocombo'
            }]
        });

        Ext.create({
            xtype: 'foopanel',
            title: 'Demo',
            bodyPadding: 10,
            renderTo: Ext.getBody()
        })
    }
});