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

unbindPopup appears to clear click event listener #455

Closed
rickhall opened this issue Mar 3, 2015 · 8 comments
Closed

unbindPopup appears to clear click event listener #455

rickhall opened this issue Mar 3, 2015 · 8 comments

Comments

@rickhall
Copy link

rickhall commented Mar 3, 2015

This issue is a copy of the following Leaflet issue:

https://github.com/Leaflet/Leaflet/issues/3177

It is not clear if this issue is in Leaflet or markercluster...

I am using Leaflet marker cluster since I'm dealing with lots of data. I do something like this to listen for clicks on markers:

this._cluster = new L.MarkerClusterGroup();
this._cluster.on(
    "click",
    bind(this._onMarkerClick, this)
);

And something like this to listen for popup close events:

this._map = new L.Map($map[0], {})
    .on(
        "popupclose",
        bind(this._onPopupClose, this)
    );

To avoid creating excessive popups, I am trying to dynamically bind/unbind popups to the markers, like this:

_onMarkerClick: function(event) {
    var $content = this._createPopupContent(event.layer._myObject);
    event.layer.bindPopup($content[0], { }).openPopup();
},
_onPopupClose: function(event) {
    if (event.popup._source) {
            event.popup._source.unbindPopup();
    }
},

This works the initial time a marker is clicked, but subsequent clicks on the marker do not trigger the event and thus the popup is not displayed. This only affects the clicked marker, but any time I click another marker and dismiss its popup, then it will no longer receive click events as well. If I do not unbind in the close event, but instead just close the popup then I continue to receive click events and my popup is displayed.

From my poking around, it appears that invoking unbindPopup() in this scenario is the equivalent to doing an "off click" for that specific marker. Perhaps I am missing something, but this doesn't really seem to be an intuitive behavior. I am simply trying to reduce memory consumption by disposing of unwanted popups.

Perhaps I am missing the more obvious way to do this, but I couldn't find anything.

@rickhall
Copy link
Author

rickhall commented Mar 3, 2015

I created this fiddle to demonstrate:

http://jsfiddle.net/fphonku5/1/

It is entirely possible I am doing something incorrectly, but to see the issue simply run and click on the marker to see the popup, click off of the popup to make it go away, then click on the marker again and you will see the popup doesn't appear again.

@janatjak
Copy link

janatjak commented Mar 5, 2015

I try fix it.

@rickhall
Copy link
Author

rickhall commented May 7, 2015

Were you able to fix this?

@janatjak
Copy link

janatjak commented May 7, 2015

I have not found a solution at this moment. I will be work on this issue at weekend.

@rickhall
Copy link
Author

rickhall commented May 7, 2015

Great, let me know and I can test it. Thanks!

@janatjak
Copy link

janatjak commented May 9, 2015

Maybe I don't understand your problem, but if remove "popupclose" handler it works for me.
I understand correctly, do you want close popups when you click on map?
http://test.ifire.cz/leaflet/

@rickhall
Copy link
Author

rickhall commented May 9, 2015

Yes, I understand that not unbinding the popup allows the popup to continue to open, that is pretty much what I say at the end of the third from last paragraph in the original description.

The issue is, I don't want to bind a popup to every marker. I was trying to be efficient and only bind a popup to a marker when it was clicked and then unbind the popup from the marker when the popup is closed. My fiddle demonstrates that this isn't possible since I no longer get click events for that marker after unbinding the popup in the close handler.

@rickhall
Copy link
Author

I will close this issue here, since I was about to recreate my issue on using just a FeatureGroup, so I think this should be tracked on my original issue in Leaflet core.

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