I'm using django-dajaxice-ng
package (django-dajaxice
fork for Django
1.5 and above, since I use Django 1.5.4
), and I have this strange issue.
Sometimes the callback function is called twice or even more.
Simple usage:
...
{% load dajaxice_templatetags %}
...
{% dajaxice_js_import %}
<script>
function update_chat(data) {
messages = eval(data.messages);
howmany=messages.length;
if (howmany>0)
console.log('called');
for (i=0;i<howmany;i++) {
M=messages[i];
console.log(M.msg,M.sent);
};
}
setInterval(function() {
Dajaxice.myapp.receiveChat(update_chat,{'userID': {{otheruser.id}} });
},2000);
</script>
and in myapp/ajax.py
:
def receiveChat(request,userID):
# find new messages from userID and mark them as READ.
return json.dumps({'messages':serialized_messages})
Here's the console log:
called
"Message" "16:09:28"
called
"Message" "16:09:28"
As you can see, sometimes (very often, actually) update_chat
is triggered 2 or 3 times.
I'm 100% sure that:
- the ajax function receiveChat is called once
- the message shown twice is stored once.
I define serialized_messages = json.dumps(messages)
, being messages
a list of dicts.
The result is like this:
'[{"msg": "Message", "sent": "16:09:28"}]'
In this case there's one only message, but works as well with more than one.
I had to do this double serialization (the other one's in return
) because apparently dajaxice accept only dict as admitted return values.
Anyone who also got this behavior? Any clue?
EDIT I'm using neo4django
which uses neo4j-rest-client
, I have to do a .save() on a model, and notice ONLY with that save I got the problem, maybe because of another HTTP request?
EDIT 2 It's not a dajaxice issue, switching to ajax $.get()
gives same results, but really I can't figure it out. Seems like not all browsers are doing it, sure Chrome does.
I'm using django-dajaxice-ng
package (django-dajaxice
fork for Django
1.5 and above, since I use Django 1.5.4
), and I have this strange issue.
Sometimes the callback function is called twice or even more.
Simple usage:
...
{% load dajaxice_templatetags %}
...
{% dajaxice_js_import %}
<script>
function update_chat(data) {
messages = eval(data.messages);
howmany=messages.length;
if (howmany>0)
console.log('called');
for (i=0;i<howmany;i++) {
M=messages[i];
console.log(M.msg,M.sent);
};
}
setInterval(function() {
Dajaxice.myapp.receiveChat(update_chat,{'userID': {{otheruser.id}} });
},2000);
</script>
and in myapp/ajax.py
:
def receiveChat(request,userID):
# find new messages from userID and mark them as READ.
return json.dumps({'messages':serialized_messages})
Here's the console log:
called
"Message" "16:09:28"
called
"Message" "16:09:28"
As you can see, sometimes (very often, actually) update_chat
is triggered 2 or 3 times.
I'm 100% sure that:
- the ajax function receiveChat is called once
- the message shown twice is stored once.
I define serialized_messages = json.dumps(messages)
, being messages
a list of dicts.
The result is like this:
'[{"msg": "Message", "sent": "16:09:28"}]'
In this case there's one only message, but works as well with more than one.
I had to do this double serialization (the other one's in return
) because apparently dajaxice accept only dict as admitted return values.
Anyone who also got this behavior? Any clue?
EDIT I'm using neo4django
which uses neo4j-rest-client
, I have to do a .save() on a model, and notice ONLY with that save I got the problem, maybe because of another HTTP request?
EDIT 2 It's not a dajaxice issue, switching to ajax $.get()
gives same results, but really I can't figure it out. Seems like not all browsers are doing it, sure Chrome does.
0 commentaires:
Enregistrer un commentaire