django-leaflet icon indicating copy to clipboard operation
django-leaflet copied to clipboard

map:init event never triggered

Open ScottEAdams opened this issue 11 years ago • 13 comments

I have not done anything out of the ordinary and for some reason the map:init event is never triggered. Using the callback works as expected. Any ideas? Best way to debug?

ScottEAdams avatar Aug 29 '13 10:08 ScottEAdams

for example:

window.addEventListener("map:init", function () { console.log("Init: 1"); }, false);

document.addEventListener("DOMContentLoaded", function () { console.log("Init: 2"); }, false);

Console shows only Init:2

My base map is showing.

ScottEAdams avatar Aug 29 '13 10:08 ScottEAdams

Thanks for reporting !

Hum strange, the source is trivial:

https://github.com/makinacorpus/django-leaflet/blob/master/leaflet/static/leaflet/leaflet.extras.js#L209-L218

Do you have CustomEvent support ?

Can you try with / without jquery ?

leplatrem avatar Aug 29 '13 11:08 leplatrem

Tried without jquery and then it tiggers. Some kind of conflict?

ScottEAdams avatar Aug 30 '13 01:08 ScottEAdams

Ok, good news, it may come from missing api. Which jQuery version are you' using ?

leplatrem avatar Aug 30 '13 10:08 leplatrem

jquery-1.10.2.min.js

ScottEAdams avatar Aug 30 '13 10:08 ScottEAdams

Could you please try to run this snippet please, and see if event is triggered and received ?

    $(window).on('bar:foo', function (e) { console.log('Notified o/ '); });

    var target = window,
         type = 'bar:foo',
         data = {a: 1};

    var evt = jQuery.Event(type);
            for (var k in data)
                evt[k] = data[k];
            jQuery(target).trigger(evt);

leplatrem avatar Aug 30 '13 10:08 leplatrem

Sorry for the late reply!

yes, I get a "Notified o/ " message in the console but only that. Should I be seeing anything else?

ScottEAdams avatar Sep 09 '13 19:09 ScottEAdams

Sorry for re-opening this. I'm getting the exact same behaviour on Safari (10.9.4). Works as expected on Firefox.

joaoponceleao avatar Jul 07 '14 12:07 joaoponceleao

Don't be sorry !

I don't have much time right now to investigate the issue :( If you want to contribute a fix, don't hesitate, I would be pleased to merge it !

leplatrem avatar Jul 21 '14 12:07 leplatrem

This was an issue for me in Safari, breaking all the maps. It seems to be a bug in how the event is created. Here is how I had to fix it for now:

    if (typeof window.CustomEvent == 'function'){
        window.addEventListener("map:init", init_handler, false);
    }else{
        $(window).on("map:init", init_handler);
    }

Time permitting, maybe I can find the source issue.

demiurg avatar Aug 17 '14 16:08 demiurg

Thanks for this feedback !

It looks like it's simple as that : CustomEvent are not widely supported : https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent#Browser_compatibility

Which leads me to think that we should maybe rewrite this piece of code in static/leaflet.extras.js. If anyone feels like to do it, I would be pleased to review and merge a PR :)

leplatrem avatar Aug 18 '14 07:08 leplatrem

Hi @demiurg

I have the same problem as yours, not working on Safari. It seems your solutions need a jquery ($(window).on("map:init", init_handler);). Do you have any solution without jquery?

I asked this since that if else statement will be place before loading any jquery.

ismailsunni avatar Jun 07 '16 01:06 ismailsunni

You can extract relevant jquery event listening code applicable to safari, from jquery.

demiurg avatar Jun 07 '16 01:06 demiurg