javascript - Handling async ajax calls within django session -


i have django application, template contains loop in javascript, iterates of check-boxes in table. each check box, send ajax request view function, want save id of check box in list or remove id list (depends on check state). need list part of request.session dictionary. results show me ajax calls asynchronous , makes list wrongly updated, , inconsistent. there thread safe data structures can store part of session, , ensure sync list updating?

javascript , ajax:

function checkall(source, type) {             checkboxes = document.getelementsbyname(type);             for(var i=0, n=checkboxes.length;i<n;i++) {                 if (checkboxes[i].checked != source.checked) {                     checkboxes[i].checked = source.checked;                     select_row(checkboxes[i], source.checked);                 }             }         }  function select_row(row_selector, is_checked) {             is_box_checked = typeof is_checked !== 'undefined' ? is_checked : row_selector.checked;             request = {                 url: "{% url 'set_check_box' %}",                 type: "post",                 contenttype: "application/x-www-form-urlencoded",                 data: {                     csrfmiddlewaretoken: "{{ csrf_token }}",                     checked: is_box_checked,                     check_box_id: row_selector.id,                     type: row_selector.name                 },                 error: function(response, status, error_msg) {                     console.log(error_msg);                 }             };             $.ajax(request);         } 

view function:

def set_check_box(request):     request.session.modified = true     check_box_list = list(request.session['connects_check_boxes_id_list'])     check_box_id = request.post["check_box_id"]     is_checked = json.loads(request.post['checked'])     if is_checked:         check_box_list.append(check_box_id)     else:         check_box_list.remove(check_box_id)     request.session['connects_check_boxes_id_list'] = list(check_box_list) return httpresponse("") 

all had set async option false part of request parameters.

function select_row(row_selector, is_checked) {             is_box_checked = typeof is_checked !== 'undefined' ? is_checked : row_selector.checked;             request = {                 url: "{% url 'set_check_box' %}",                 type: "post",                 contenttype: "application/x-www-form-urlencoded",                 async: false,                 data: {                     csrfmiddlewaretoken: "{{ csrf_token }}",                     checked: is_box_checked,                     check_box_id: row_selector.id,                     type: row_selector.name                 },                 error: function(response, status, error_msg) {                     console.log(error_msg);                 }             };             $.ajax(request);         } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -