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

die*_*and 6 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
})
Run Code Online (Sandbox Code Playgroud)

谢谢.

Lio*_*han 25

如果您想要仅触发一次事件,请使用: -

Ext.getCmp('myBtn').on('click', function(){
    alert('Alert this message only in first click.');
}, null, {single: true});
Run Code Online (Sandbox Code Playgroud)

{single:true}将使这个匿名函数在调度后正好运行一次,所以这应该完成你的要求.

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

查看ExtJS Events @ http://docs.sencha.com/extjs/5.0.0/core_concepts/events.html

另请查看事件选项@ http://docs-origin.sencha.com/extjs/5.0.0/apidocs/#!/api/Ext.mixin.Observable-method-on

快乐的ExtJS :)


Tom*_*mmi 10

如果你命名你的匿名函数会有用吗?此外,您可以从方法调用中获取对该按钮的引用,因此您无需使用Ext.getCmp().

Ext.getCmp('myBtn').on('click', function handleClick(button, event){
    alert('Alert this message only in first click.');
    button.removeListener('click', handleClick);
})
Run Code Online (Sandbox Code Playgroud)

  • 我使用你的解决方案,短期的removeListener - > button.un('click',handleClick); (2认同)

Abd*_*oof 5

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

var myfunction = function() {

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

Ext.getCmp('myBtn').on('click',myfunction);
Run Code Online (Sandbox Code Playgroud)