extjs button scope

大憨熊 提交于 2019-12-11 02:28:50

问题


I am trying to understand scope in the below scenario. When calling searchTerms, this under the scope:this refers to the searchTerms function rather than the panel itself. It seems to be different from what I observe from other examples. May I know what mistakes did i make?

function searchTerms(){
       var searchGrid = new Ext.grid.GridPanel({

        });

        var searchPanel = new Ext.form.FormPanel({
            region: 'south',
            height:150,
            items:[
            {
                xtype: 'textfield',
                fieldLabel: 'Keywords',
            },{
                xtype: 'textfield',
                fieldLabel: 'Label',
            },{
                xtype: 'datefield',
                fieldLabel: 'Valid till'
            },new Ext.Button({
                text: 'crawl',
                scope: this,
                handler: function(b,e){
                    Ext.Ajax.request({^M
                        url: '/discovery/tsearch',^M
                        params: {^M
                            keywords: this.items[0].getValue(),
                            label: this.items[1].getValue(),
                            valid: this.items[2].getValue(),
                        },
                    });
                }
            }),],
        });

        var regionPanel = new Ext.Panel({
            title: 'search',
            layout: 'border',
            items: [searchPanel, searchGrid]
        });

    return regionPanel;
}

回答1:


I think you meant to do this:

function searchTerms(){
   var searchGrid = new Ext.grid.GridPanel({

    });

    var searchPanel = new Ext.form.FormPanel({
        region: 'south',
        height:150,
        items:[
        {
            xtype: 'textfield',
            fieldLabel: 'Keywords',
        },{
            xtype: 'textfield',
            fieldLabel: 'Label',
        },{
            xtype: 'datefield',
            fieldLabel: 'Valid till'
        }],
    });
    searchPanel.add(new Ext.Button({
            text: 'crawl',
            scope: searchPanel,
            handler: function(b,e){
                Ext.Ajax.request({
                    url: '/discovery/tsearch',
                    params: {
                        keywords: this.items[0].getValue(),
                        label: this.items[1].getValue(),
                        valid: this.items[2].getValue(),
                    },
                });
            }
        })
    );
    var regionPanel = new Ext.Panel({
        title: 'search',
        layout: 'border',
        items: [searchPanel, searchGrid]
    });

return regionPanel;
}


来源:https://stackoverflow.com/questions/8588968/extjs-button-scope

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!