In ExtJS, How to suddenly untie a fire event?

The goal is to display a warning window only for the first time.

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 }) 

Thanks.

+6
source share
3 answers

Would this work if you named your anonymous function? In addition, you can get a link to a button from a method call, so you do not need to use Ext.getCmp() .

 Ext.getCmp('myBtn').on('click', function handleClick(button, event){ alert('Alert this message only in first click.'); button.removeListener('click', handleClick); }) 
+10
source

If you want the event to fire exactly once, use the following: -

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

{single:true} will make this anonymous function run exactly once after sending, so this should fulfill your requirements.

Note. null is a really useful changer. You may need this to change the scope in an anonymous function

Check out ExtJS Events @ http://docs.sencha.com/extjs/5.0.0/core_concepts/events.html

Also check event parameters @ http://docs-origin.sencha.com/extjs/5.0.0/apidocs/#!/api/Ext.mixin.Observable-method-on

Happy ExtJS :)

+25
source

Try working with a named function, not an anonymous function.

 var myfunction = function() { alert('Alert this message only in first click.'); Ext.getCmp('myBtn').un('click', myfunction); } Ext.getCmp('myBtn').on('click',myfunction); 
+5
source

Source: https://habr.com/ru/post/886460/


All Articles