Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ExtJS:全局函数在页面加载之前执行。如何延迟?_Javascript_Function_Extjs_Global_Listeners - Fatal编程技术网

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

Javascript ExtJS:全局函数在页面加载之前执行。如何延迟?,javascript,function,extjs,global,listeners,Javascript,Function,Extjs,Global,Listeners,我在面板项中有一个组合框,并在组合框侦听器中放置了一个选择事件。事件的函数调用全局方法,但此全局函数在页面加载期间执行 我在几个类上使用这个组合框选择方法。因此,全局函数将变得非常简单。因此,在combobox的项目选择过程中,需要与正常行为一起使用。我如何才能实现这种调整 全球职能: Ext.define('MyApp.BaseMethods', { requires: [], singleton: true, // Instead of using method c

我在面板项中有一个组合框,并在组合框侦听器中放置了一个选择事件。事件的函数调用全局方法,但此全局函数在页面加载期间执行

我在几个类上使用这个组合框选择方法。因此,全局函数将变得非常简单。因此,在combobox的项目选择过程中,需要与正常行为一起使用。我如何才能实现这种调整

全球职能:

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;
    },
});
和小组:

在运行此方法时;全局函数在页面加载期间运行,单击按钮显示FooPanel及其项,并给出错误,因为无法选择组合项

Uncaught TypeError: Cannot read property 'getSelectedRecord' of undefined
如何延迟

不需要拖延,只需要改变方式

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

我在几个类上使用这个组合框选择方法。因此,全局函数将变得非常简单。因此,在combobox的项目选择过程中,需要与正常行为一起使用。我如何才能实现这种调整

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

范例

{
    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');
        }
    }
}
在这里,我创建了一个演示。希望这将帮助/指导您实现您的要求

代码片段


亲爱的@N.贾达夫,这是一个非常清楚的解释。谢谢。
{
    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');
        }
    }
}
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()
        })
    }
});