English 中文(简体)
Ext JS范围问题
原标题:Ext JS scope issue

这是旧代码,但无论如何。。。

I am trying to filter a store, and listen to the event in a comboBox, so I can refresh it. My doQuery event didnt work, ( well actually it did work, but returned random result sets, leaving an overall wtf feeling )

    config.store.filterBy(function Filter(record){
    //this works
        if (record.data.field != ""){
            return true;
        }
        else {return false;}
    });

However this does not automagically update the combobox. So I tried various versions of

    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()不应该有任何范围问题。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签