从ExtJS4组合框侦听器调用函数
我在ExtJS4中从组合框侦听器调用自定义函数时遇到问题。 我已经设置了组合框侦听器,如下所示从ExtJS4组合框侦听器调用函数,extjs,extjs4,extjs-mvc,Extjs,Extjs4,Extjs Mvc,我在ExtJS4中从组合框侦听器调用自定义函数时遇到问题。 我已经设置了组合框侦听器,如下所示 listeners:{ select:{ fn:function(combo, value) { this.test; } } } 我的自定义函数是在关闭 this.callParent(arguments); 我的自定义函数是 test: function(){ alert('test'); }
listeners:{
select:{
fn:function(combo, value) {
this.test;
}
}
}
我的自定义函数是在关闭
this.callParent(arguments);
我的自定义函数是
test: function(){
alert('test');
}
我做错了什么?您的作用域错误,选择函数中的
此
指向您的组合,而您的测试函数所在的对象可能没有。此外,如果调用一个函数,则需要在其周围使用大括号:this.test()
或许最好采用以下方式:
initComponent: function() {
var me = this;
me.items = [//...
{
xtype: 'boxselect',
//props...
listeners: {
select:{
fn:function(combo, value) {
this.test(); // this will now be the this variable that you give to your scope.
//Or simply: me.test();
}
},
scope: me
}
}
//...
];
me.callParent();
}
您可能想发布更多的代码,因为我们需要知道测试函数的范围和组合框的范围。下面是我的代码
initComponent: function() {
this.items = [
{
xtype: 'form',
padding: '5 5 0 5',
autoScroll:true,
border: false,
style: 'background-color: #fff;',
items: [
{
xtype:'fieldset',
columnWidth: 0.5,
id:'adfieldset',
title: 'Ad Details',
collapsible: true,
defaultType: 'textfield',
defaults: {anchor: '100%'},
layout: 'anchor',
items: [
{
xtype : 'boxselect',
store : 'product.CategoryComboBox',
name: 'category[]',
id:'category',
displayField: 'name',
valueField: 'idProductCategory',
multiSelect : false,
fieldLabel: 'Category',
//allowBlank: false,
allowQueryAll : false,
multiSelect : true,
forceSelection : true,
typeAhead: true,
triggerAction: 'all',
delimiter : ',',
width: 300,
queryMode:'local',
listeners:{select:{fn:function(combo, value) {
this.test;
代码不完整,但我非常确定这是一个范围问题,正如我下面的回答。那么有没有办法解决范围问题。请让我知道。我发布了一个解决方案,应该可以,你至少试过了吗?