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

Cannot get reference to map loaded in admin pages. #347

Open
kysevenle opened this issue Jan 17, 2022 · 1 comment
Open

Cannot get reference to map loaded in admin pages. #347

kysevenle opened this issue Jan 17, 2022 · 1 comment

Comments

@kysevenle
Copy link

kysevenle commented Jan 17, 2022

I'm trying to manipulate the admin map after it is loaded however I cannot get a reference to the map. I've set NO_GLOBAL to false, and my template to override the admin template is as follows.

Template:

{% extends "admin/change_form.html" %}
{% load i18n admin_urls static leaflet_tags %}

{% block content %}{{ block.super }}
<script>
    map = window['leafletmapid_location-map'];
    console.log(map);
</script>
{% endblock %}

This logs "undefined", but if I load the page, open the console, and run the exact same code I get a reference to the map, and can then add markers as needed. Am I going about this the wrong way? Should I be able to get a reference to the map this way?

I've tried placing the script tag into the admin_change_form_document_ready block.

I've also tried the following with the same results.

<script>
    $(document).ready(function() {
        console.log(window['leafletmapid_location-map']);
    });
</script>

Edit:
It appears the issue is that the map is not initialized by the time var map = window['leafletmapid_location-map']; runs. If I run it in an event listener after a button click it works, but it is in the scope of the listener function.

I cannot find anyway to get a reference to the admin map in the global scope of my template. Is there anyway to accomplish this?

@Gagaro
Copy link
Member

Gagaro commented Jan 28, 2022

The map is probably not initialized yet. Try overwriting the callback block in leafet/admin/widget.html, you should have the map variable there.

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