Yimin Rong
Yimin Rong

Reputation: 2027

ExtJS 5.0 - Calling un or removeEventListener while in listener?

Some of our legacy code creates listeners with on and anonymous functions. These listeners have a finite lifespan.

Code is something like this:

me.getEl().on(someCondition()? 'mousedown': 'touchstart',
    function(event) {
        if (someOtherCondition()) {
            destroyStuff();
            // We don't need this listener any more!
            // ***
        }
    }
);

Checking the resource usage as the application runs, these zombie listeners accumulate and, especially the mouseover ones, use up a lot of resources and cycles.

Is there a way to use un or removeEventListener below the // *** comment and remove the listener? Say something along these lines:

event.target.removeEventListener(event.type, /* reference this anonymous function? */);

Upvotes: 0

Views: 334

Answers (1)

Alexander
Alexander

Reputation: 20234

You have multiple options.

  • on takes an options parameter that may contain single:true if it should only be executed once. I think this would not work for your example because the listener can be called multiple times, but possibly for some of your use cases that do not have a condition.
  • You can use mon with destroyable: true to create destroyable listeners. These listeners can be removed using mun.

Upvotes: 3

Related Questions