Y
Y
Yeldos Adetbekov2016-02-02 18:41:03
JavaScript
Yeldos Adetbekov, 2016-02-02 18:41:03

Append("some jquery functionon") not working in jquery when getting them with ajax?

Good day. There was a problem updating content dynamically. There is django-server and ajax requests using setInterval() pull data from the server. So in django-templates I prepared a template with jquery code and when sending from the server to ajax, I render using .append() or .html(). Everything works fine on page refresh. But it seems to me that what I supposedly srendrilled is how the text falls into the dom, but JQ does not execute it because it is not a "console" type, but it only executes the things that are in front of it. I understand correctly?
Okay, here's the actual django code:

from django_ajax.decorators import ajax
from message.models import Message
from django.utils import timezone

@ajax
def index(request):
    context={
        "friends":[],
        "messages":[],
    }
    index=0
    if Message.objects.unread_messages_count(request.user)!=0:
        for message in Message.objects.filter(to_user=request.user,viewed__isnull=True,messaged__isnull=True):
            index+=1
            context["messages"].append({
                "message":message.message[:255],
                "user":message.from_user.get_full_name,
                "duration":index*2200+6000
            })
            message.messaged=timezone.now()
            message.save()
        return render(request, 'message.html',context)

I call data with pom. agex:
<script src="{% static 'scripts/js/jquery-latest.js' %}"></script>
<script type="text/javascript" src="{% static 'django_ajax/js/jquery.ajax.min.js' %}"></script>
<script src="{% static 'scripts/js/jquery.growl.js' %}"></script>
<link rel="stylesheet" href="{% static 'scripts/css/jquery.growl.css'%}">

<script>
    function ajax_updates() {
        ajaxGet("{% url 'ajax_updates' %}",function(content){
                $('#notifications').append(content);
        })
    }
    setInterval(ajax_updates, 5000);
    ajax_updates();
</script>
<script id="notifications"></script>

When everything is loaded(On page load), everything works fine. But when the page is already loaded and javascript has nothing left to do except go further and wait for new events, but my append, as it were, in the executed fragment hangs code that is simply not viewed and not executed by JQ.
Here is the actual message.html template code:
{% for message in messages %}
  $.growl({ title: "{{message.user}}", message: "{{ message.message }}",duration: {{message.duration}}});
{% endfor %}

I need your help! I will be very grateful!))

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question