在ExtJS中,如何在事件触发时突然解除绑定?

在ExtJS中,如何在事件触发时突然解除绑定?,extjs,event-handling,Extjs,Event Handling,目标是仅在第一次单击时显示警报框 Ext.getCmp('myBtn').on('click', function(){ alert('Alert this message only in first click.'); Ext.getCmp('myBtn').un('click', this); // my attempt, still not work }) 谢谢。尝试使用命名函数而不是匿名函数 var myfunction = function() { alert

目标是仅在第一次单击时显示警报框

Ext.getCmp('myBtn').on('click', function(){
    alert('Alert this message only in first click.');
    Ext.getCmp('myBtn').un('click', this); // my attempt, still not work
})

谢谢。

尝试使用命名函数而不是匿名函数

var myfunction = function() {

   alert('Alert this message only in first click.');
   Ext.getCmp('myBtn').un('click', myfunction);
}

Ext.getCmp('myBtn').on('click',myfunction);

如果命名匿名函数,它会工作吗?另外,您可以从方法调用中获取对按钮的引用,因此不需要使用
Ext.getCmp()


如果希望事件只触发一次,请使用以下命令:-

Ext.getCmp('myBtn').on('click', function(){
    alert('Alert this message only in first click.');
}, null, {single: true});
{single:true}
将使这个匿名函数在分派后只运行一次,因此这应该可以满足您的要求

注意:
null
实际上是有用的范围变换器。您可能需要这样来更改匿名函数中的作用域

查看ExtJS事件@

同时检查事件选项@


快乐的ExtJS:)

也工作吧。感谢您的快速响应。我使用您的解决方案,短期内只需重新设置Listener->button.un('click',handleClick);
Ext.getCmp('myBtn').on('click', function(){
    alert('Alert this message only in first click.');
}, null, {single: true});