我对按钮的作用域有问题'的听众

I am having troubles with the scope of a button's listener

本文关键字:有问题 按钮 作用域      更新时间:2024-03-10

在执行了一个与窗口不在同一文件中定义的函数后,我试图关闭一个窗口。

我将尝试解释所涉及的类的结构:

有两个类,Class1.jsClass2.jsunction1(){},在Class2.js 中定义function 2(){}

工作原理:

函数2它在Class1.js中被调用,打开一个窗口,它作为参数函数1接收,比如:

函数2(函数1);

函数1当点击新窗口上的按钮时执行,也在Class2.js:中定义

var win = new Ext.Window({  
    id: 'win that I want to close',
    .  
    .  
    .   
    buttons: [{  
              id: 'button that activates function1',  
              text: 'button1',  
              listeners: {  
                    click:  function1  
                  },  
                  scope: win  
              },
              .
              .
              .
             ]
});  
win.show();

在Class1.js中,在函数1中,我尝试做:

if(this.id == 'win that I want to close'){
    this.close();
}

但是我得到的范围是来自新窗口中按钮的范围,而不是窗口的范围,所以我无法关闭它。

我知道我的解释很糟糕,但不容易解释,我完全陷入了困境。

祝贺你,如果你试图不理解我写的东西,你是非常勇敢的!谢谢你的帮助!

我有解决方案:

listeners: {
    'click': {
        fn: function1,
        scope: {
            winid: 'win that I want to close'
        }
    }
}

用它可以从CCD_ 2中得到具有CCD_ 1的元素。