Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

event listeners on a layer should be dormant when the layer is removed, and re-activated when the same layer is re-added #5145

Closed
andrewharvey opened this issue Aug 14, 2017 · 1 comment

Comments

@andrewharvey
Copy link
Collaborator

andrewharvey commented Aug 14, 2017

For the background see https://gis.stackexchange.com/questions/251921/how-to-remove-events-map-onclick-from-mapbox-gl

If a user add's event listeners to a layer with map.on(type, layer, listener) and then removes that layer without having removed the event listeners with map.off, GL JS will fire an error when that event listener tries to trigger.

I can't think of any use cases for GL JS to not removing any active event listeners when calling removeLayer.

mapbox-gl-js version: 0.39.1

Steps to Trigger Behavior

  1. addLayer
  2. register an event listener on that layer
  3. remove the layer without removing the event listeners with off

https://jsbin.com/jecixe/edit?html,console,output

Expected Behavior

Upon removeLayer, GL JS would automatically remove any event listeners.

Actual Behavior

"The layer '' does not exist in the map's style and cannot be queried for features."
@jfirebaugh
Copy link
Contributor

I wouldn't expect it to produce an error, but I also wouldn't expect it to deregister the event listener. Instead, I'd expect the event listener to simply remain dormant until a layer with a matching ID was added again.

The analog this API is modeled on is jQuery delegated events, which works that way. In fact, you can add a delegated event listener prior to there being any matching descendants / layers, which is sometimes handy.

@andrewharvey andrewharvey changed the title removeLayer should remove any event listeners still active on the layer event listeners on a layer should be dormant when the layer is removed, and re-activated when the same layer is re-added Aug 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants