Tuesday, August 4, 2015

Re: Passing parameters / attributes to javascript in a template

I don't quite follow what you're doing with set_first_call_true and set_first_call_false. Are you using that to control the for loop behavior?

If you are, take a look at https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#for. Django's template language has that built in.

If you're pushing to an array like in your initial example, you don't need to handle commas at all. Here's an example out of Chrome's dev tools:

> var a = []
> a.push('foo')
> a.push('bar')
> a
< ["foo", "bar"]

If you want to define it in one go, try something more like:

<script type="text/javascript">
    my_markers = [
    {% for current_addr in mymodel.addresses.all %}
        {
            lat: {{ current_addr.lat }},
            lon: {{ current_addr.lon }},
            description: {{ current_addr.description | safe }}
        }{% if not forloop.last %}, {% endif %}
    {% endfor %}
    ];
</script>

On Tue, Aug 4, 2015 at 10:53 AM, Déborah Leder <debdeb312@gmail.com> wrote:
After some more tests, I have managed to understand where my problem is (but I don't know how to resolve it).
Here is my final piece of code :
<script type="text/javascript" src="{% static "js/geomap_var.js" %}"></script>

<script type="text/javascript">
    my_markers
= [
   
{{ mymodel.set_first_call_true }}
   
{% for current_addr in mymodel.addresses.all %}

       
{% if not mymodel.first_call %},{% endif %}
       
{
            lat
: {{ current_addr.lat }},
            lon
: {{ current_addr.lon }},
            description
: {{ current_addr.description | safe }}
       
}

   
{{ mymodel.set_first_call_false }}
   
{% endfor %}
   
];
</script>
   
<script type="text/javascript" src="{% static "js/geomap_body.js" %}"></script>


I had to add an attribute "first_call" to mymodel, so that my javascript declaration was correct (in an array, we should always write the comma between 2 elements, but not after each element or before each element).
The only problem is that every time I call mymodel.set_first_call_true or mymodel.set_first_call_false, it inserts a "none" inside my javascript code.
Is it possible to "delete" the "non" statement ?


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/232757e4-0a4b-450f-808d-764453783dea%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Bv0ZYVp8Z1S7qXq%3DjELWpWQKd4-KjTw5DX2ZDdTedxKtuja5Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment