diewland
diewland

Reputation: 1915

In ExtJS, How to suddenly unbind when event fire?

Goal is to show alert box only first time click.

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

Thank you.

Upvotes: 7

Views: 6787

Answers (3)

Lionel Chan
Lionel Chan

Reputation: 8069

If you want an event to be triggered exactly once, use this:-

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

The {single:true} will make this anonymous function to be run exactly once after dispatched, so this should accomplish your requirement.

Note: The null is actually the useful scope changer. You might need this to change the scope in the anonymous function

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

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

Happy ExtJS :)

Upvotes: 26

Tommi
Tommi

Reputation: 8608

Would it work if you named your anonymous function? Also, you can get reference to the button from the 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);
})

Upvotes: 10

Abdel Raoof Olakara
Abdel Raoof Olakara

Reputation: 19353

Try working with a named function rather than 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);

Upvotes: 5

Related Questions