Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to unregister .on(drawend,….) event

as shown in the below posted code, i have an event #drawEvt and it is registered for drawend. the problem i have is about unregistering the event. as shown below in the code
in method deattachAll() is how i unregister it, but it seems it does not work because the logs iinside the body of the evtListenerOnDrawEnd gets displayed several times.

one more imporatant note, is the algorithm is designed so that digitize() is being called after deattachAll(). in other words, initially digitize() is called and will never be called
again for a second time unless it is preceeded by a call to deattachAll()

please let me why the listener #evtListenerOnDrawEnd is invoked several times and how to fix it

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

code

#evtListenerOnDrawEnd = (evt, mapBuilderInstance, rasterLayer) => {
    ...
    ...
    ...
}

getDrawGeomForSource(src, geomType) {
    return new Draw({
        source: src,
        type: geomType,
    });
}

his.#drawEvt = this.#mapBuilderInstance.getDrawGeomForSource(this.#vectorSource, 'Polygon');// to init this.`#drawEvt`

digitize() {
    this.#drawEvt.on('drawend', (evt) => this.#evtListenerOnDrawEnd(
        evt, this.#mapBuilderInstance, this.#rasterLayer
    ));
}

deattachAll() {
    this.#unregisterEvtListeners();
}

#unregisterEvtListeners() {
    this.#mapBuilderInstance.unregisterEvtListener('drawend', this.#drawEvt, this.#evtListenerOnDrawEnd);
}

unregisterEvtListener(eventName, type, callbackListener) {
    if (type) {
        type.un(eventName, callbackListener);
    }
}

>Solution :

If the callback is a named function you can use

draw.on('drawend', handler);

draw.un('drawend', handler);

If it is an anonymous function you will need

key = draw.on('drawend', handler);

unByKey(key);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading