Refactor server communication & request state handling to be DRY
There are at least several hundred different AJAX code blocks in the JS. Sometimes, Angular Factories help abstract the actual AJAX handling (see: report app). Sometimes, every request is handwritten (see: experiments app). Many requests need to be set up beforehand. One common setting is sending the CSRF token:
$.ajaxSetup({
beforeSend: function(xhr, settings) {
var csrftoken = $.cookie('csrftoken');
xhr.setRequestHeader('X-CSRFToken', csrftoken);
}
});
Requests may also handle certain error codes:
$.ajax({
//....
statusCode: {
404: function(data, status, message) {
container.textContent = message + ' (' + data.status + ')';
$(container).addClass('error');
$(container).removeClass('progress');
},
500: function(data, status, message) {
container.textContent = message + ' (' + data.status + ')';
$(container).addClass('error');
$(container).removeClass('progress');
},
}
//....
Since many requests most likely have similar settings/error handling, refactoring the actual requesting functionality (into it's own Angular Service) would make requests more consistent and DRY. It would also help separate the actual AJAX from the handling of a successful request.