Made post upload stop button abort current AJAX

This commit is contained in:
Marcin Kurczewski 2014-10-18 10:31:01 +02:00
parent 7e1b901a84
commit dd8050f250
2 changed files with 25 additions and 4 deletions

View file

@ -3,6 +3,11 @@ var App = App || {};
App.API = function(_, jQuery, promise, appState) {
var baseUrl = '/api/';
var AJAX_UNSENT = 0;
var AJAX_OPENED = 1;
var AJAX_HEADERS_RECEIVED = 2;
var AJAX_LOADING = 3;
var AJAX_DONE = 4;
function get(url, data) {
return request('GET', url, data);
@ -24,8 +29,9 @@ App.API = function(_, jQuery, promise, appState) {
var fullUrl = baseUrl + '/' + url;
fullUrl = fullUrl.replace(/\/{2,}/, '/');
return promise.make(function(resolve, reject) {
jQuery.ajax({
var xhr = null;
var apiPromise = promise.make(function(resolve, reject) {
xhr = jQuery.ajax({
headers: {
'X-Authorization-Token': appState.get('loginToken') || '',
},
@ -46,6 +52,8 @@ App.API = function(_, jQuery, promise, appState) {
data: data,
});
});
apiPromise.xhr = xhr;
return apiPromise;
}
function stripMeta(data) {
@ -62,7 +70,13 @@ App.API = function(_, jQuery, promise, appState) {
get: get,
post: post,
put: put,
delete: _delete
delete: _delete,
AJAX_UNSENT: AJAX_UNSENT,
AJAX_OPENED: AJAX_OPENED,
AJAX_HEADERS_RECEIVED: AJAX_HEADERS_RECEIVED,
AJAX_LOADING: AJAX_LOADING,
AJAX_DONE: AJAX_DONE,
};
};

View file

@ -23,6 +23,7 @@ App.Presenters.PostUploadPresenter = function(
var fileDropper;
var interactionEnabled = true;
var currentUploadId = null;
var currentUploadXhr = null;
function init(params, loaded) {
topNavigationPresenter.select('upload');
@ -533,6 +534,9 @@ App.Presenters.PostUploadPresenter = function(
function stopUpload() {
currentUploadId = null;
showUploadError('Upload stopped.');
if (currentUploadXhr && currentUploadXhr.readystate !== api.AJAX_DONE) {
currentUploadXhr.abort();
}
}
function uploadNextPost() {
@ -566,7 +570,10 @@ App.Presenters.PostUploadPresenter = function(
return;
}
promise.wait(api.post('/posts', formData))
var apiPromise = api.post('/posts', formData);
currentUploadXhr = apiPromise.xhr;
promise.wait(apiPromise)
.then(function(response) {
$row.slideUp(function(response) {
if (priorUploadId === currentUploadId) {