问题描述
|
这是古老的代码,但无论如何...
我正在尝试过滤商店,并在comboBox中监听事件,因此可以刷新它。
我的doQuery事件没有起作用,(实际上它确实起作用了,但是返回了随机结果集,留下了总体wtf的感觉)
config.store.filterBy(function Filter(record){
//this works
if (record.data.field != \"\"){
return true;
}
else {return false;}
});
但是,这不会自动更新组合框。
所以我尝试了各种版本的
cbx = new Ext.getCmp(this);
debugger; //scope right here
this.getStore().on(\"datachanged\",function refresh(){
cbx.reset();// store\'s scope
});
但是cbx的范围似乎始终是商店,而不是组合框。
有人知道如何将存储中的数据更改事件的侦听器添加到comboBox吗?
解决方法
事件回调的默认范围是触发事件的对象。在这种情况下,那将是商店。 on()方法的第三个参数允许您覆盖范围。将第三个参数设置为
this
即可。
从文档(3.x和4.x)中:
on( String eventName,Function handler,[Object scope],[Object options] )
所以这样的事情应该可以解决问题:
this.getStore().on(\"datachanged\",function refresh() {
cbx = Ext.getCmp(this);
cbx.reset();
},this); // <-- Provide scope for the callback function
,您始终可以使用Function.createDelegate在ExtJS(我假设您正在使用ExtJS 3.x)中设置函数的范围。
this.getStore().on(\"datachanged\",function() {
cbx.reset();
}).createDelegate(this);
尽管从您提供的代码示例中,我不确定是什么问题,因为cbx.reset()应该没有任何范围问题。