0

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

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);
 }
}
asked Sep 8, 2023 at 8:33

1 Answer 1

1

If the callback is a named function you can use

handler = (evt) => { /* do something */ };
draw.on('drawend', handler);
draw.un('drawend', handler);

If it is an anonymous function you will need

key = draw.on('drawend', (evt) => { /* do something */ });
unByKey(key);
answered Sep 8, 2023 at 8:48
Sign up to request clarification or add additional context in comments.

2 Comments

hi, i added a skelton how the #evtListenerOnDrawEnd looks like. would you please have a look and tell me which approach of the ansewer you posted i have to use?
i tried the second appoach you posted, the one with unbykey and it worked

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.